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" |
| 11 | |
Daniel Dunbar | c22e0b2 | 2009-08-14 03:48:55 +0000 | [diff] [blame] | 12 | #include "llvm/CodeGen/AsmPrinter.h" |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 13 | #include "llvm/MC/MCContext.h" |
Daniel Dunbar | abde298 | 2009-07-01 06:35:03 +0000 | [diff] [blame] | 14 | #include "llvm/MC/MCInst.h" |
Chris Lattner | f9bdedd | 2009-08-10 18:15:01 +0000 | [diff] [blame] | 15 | #include "llvm/MC/MCSectionMachO.h" |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 16 | #include "llvm/MC/MCSymbol.h" |
| 17 | #include "llvm/MC/MCValue.h" |
Torok Edwin | c25e758 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 18 | #include "llvm/Support/ErrorHandling.h" |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 19 | #include "llvm/Support/raw_ostream.h" |
Torok Edwin | c25e758 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 20 | |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 21 | using namespace llvm; |
| 22 | |
| 23 | namespace { |
| 24 | |
| 25 | class MCAsmStreamer : public MCStreamer { |
| 26 | raw_ostream &OS; |
Daniel Dunbar | c22e0b2 | 2009-08-14 03:48:55 +0000 | [diff] [blame] | 27 | |
| 28 | AsmPrinter *Printer; |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 29 | |
| 30 | MCSection *CurSection; |
| 31 | |
| 32 | public: |
Daniel Dunbar | c22e0b2 | 2009-08-14 03:48:55 +0000 | [diff] [blame] | 33 | MCAsmStreamer(MCContext &Context, raw_ostream &_OS, AsmPrinter *_AsmPrinter) |
| 34 | : MCStreamer(Context), OS(_OS), Printer(_AsmPrinter), CurSection(0) {} |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 35 | ~MCAsmStreamer() {} |
| 36 | |
| 37 | /// @name MCStreamer Interface |
| 38 | /// @{ |
| 39 | |
| 40 | virtual void SwitchSection(MCSection *Section); |
| 41 | |
| 42 | virtual void EmitLabel(MCSymbol *Symbol); |
| 43 | |
Kevin Enderby | f96db46 | 2009-07-16 17:56:39 +0000 | [diff] [blame] | 44 | virtual void EmitAssemblerFlag(AssemblerFlag Flag); |
Kevin Enderby | a5c7832 | 2009-07-13 21:03:15 +0000 | [diff] [blame] | 45 | |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 46 | virtual void EmitAssignment(MCSymbol *Symbol, const MCValue &Value, |
| 47 | bool MakeAbsolute = false); |
| 48 | |
| 49 | virtual void EmitSymbolAttribute(MCSymbol *Symbol, SymbolAttr Attribute); |
| 50 | |
Kevin Enderby | 95cf30c | 2009-07-14 18:17:10 +0000 | [diff] [blame] | 51 | virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue); |
| 52 | |
Kevin Enderby | 7114824 | 2009-07-14 21:35:03 +0000 | [diff] [blame] | 53 | virtual void EmitLocalSymbol(MCSymbol *Symbol, const MCValue &Value); |
| 54 | |
Chris Lattner | 4e4db7a | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 55 | virtual void EmitCommonSymbol(MCSymbol *Symbol, unsigned Size, |
Chris Lattner | 1fc3d75 | 2009-07-09 17:25:12 +0000 | [diff] [blame] | 56 | unsigned Pow2Alignment, bool IsLocal); |
Chris Lattner | 4e4db7a | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 57 | |
Chris Lattner | 9be3fee | 2009-07-10 22:20:30 +0000 | [diff] [blame] | 58 | virtual void EmitZerofill(MCSection *Section, MCSymbol *Symbol = NULL, |
| 59 | unsigned Size = 0, unsigned Pow2Alignment = 0); |
| 60 | |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 61 | virtual void EmitBytes(const StringRef &Data); |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 62 | |
| 63 | virtual void EmitValue(const MCValue &Value, unsigned Size); |
| 64 | |
Daniel Dunbar | 84a2926 | 2009-06-24 19:25:34 +0000 | [diff] [blame] | 65 | virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0, |
| 66 | unsigned ValueSize = 1, |
| 67 | unsigned MaxBytesToEmit = 0); |
| 68 | |
| 69 | virtual void EmitValueToOffset(const MCValue &Offset, |
| 70 | unsigned char Value = 0); |
| 71 | |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 72 | virtual void EmitInstruction(const MCInst &Inst); |
| 73 | |
| 74 | virtual void Finish(); |
| 75 | |
| 76 | /// @} |
| 77 | }; |
| 78 | |
| 79 | } |
| 80 | |
Daniel Dunbar | ad4555c | 2009-07-31 23:04:32 +0000 | [diff] [blame] | 81 | /// Allow printing symbols directly to a raw_ostream with proper quoting. |
| 82 | static inline raw_ostream &operator<<(raw_ostream &os, const MCSymbol *S) { |
Daniel Dunbar | c22e0b2 | 2009-08-14 03:48:55 +0000 | [diff] [blame] | 83 | S->print(os); |
| 84 | |
| 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 | |
| 92 | return os; |
| 93 | } |
| 94 | |
Daniel Dunbar | 304f6a4 | 2009-06-25 21:03:18 +0000 | [diff] [blame] | 95 | static inline int64_t truncateToSize(int64_t Value, unsigned Bytes) { |
| 96 | assert(Bytes && "Invalid size!"); |
| 97 | return Value & ((uint64_t) (int64_t) -1 >> (64 - Bytes * 8)); |
| 98 | } |
| 99 | |
| 100 | static inline MCValue truncateToSize(const MCValue &Value, unsigned Bytes) { |
| 101 | return MCValue::get(Value.getSymA(), Value.getSymB(), |
Daniel Dunbar | 7908a6a | 2009-06-29 19:51:00 +0000 | [diff] [blame] | 102 | truncateToSize(Value.getConstant(), Bytes)); |
Daniel Dunbar | 304f6a4 | 2009-06-25 21:03:18 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 105 | void MCAsmStreamer::SwitchSection(MCSection *Section) { |
| 106 | if (Section != CurSection) { |
| 107 | CurSection = Section; |
| 108 | |
Chris Lattner | 93b6db3 | 2009-08-08 23:39:42 +0000 | [diff] [blame] | 109 | // FIXME: Needs TargetAsmInfo! |
| 110 | Section->PrintSwitchToSection(*(const TargetAsmInfo*)0, OS); |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 111 | } |
| 112 | } |
| 113 | |
| 114 | void MCAsmStreamer::EmitLabel(MCSymbol *Symbol) { |
Daniel Dunbar | 71d259b | 2009-06-24 17:00:42 +0000 | [diff] [blame] | 115 | assert(Symbol->getSection() == 0 && "Cannot emit a symbol twice!"); |
| 116 | assert(CurSection && "Cannot emit before setting section!"); |
| 117 | assert(!getContext().GetSymbolValue(Symbol) && |
| 118 | "Cannot emit symbol which was directly assigned to!"); |
| 119 | |
Daniel Dunbar | ad4555c | 2009-07-31 23:04:32 +0000 | [diff] [blame] | 120 | OS << Symbol << ":\n"; |
Daniel Dunbar | 71d259b | 2009-06-24 17:00:42 +0000 | [diff] [blame] | 121 | Symbol->setSection(CurSection); |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 122 | Symbol->setExternal(false); |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 123 | } |
| 124 | |
Kevin Enderby | f96db46 | 2009-07-16 17:56:39 +0000 | [diff] [blame] | 125 | void MCAsmStreamer::EmitAssemblerFlag(AssemblerFlag Flag) { |
| 126 | switch (Flag) { |
Daniel Dunbar | 011e4db | 2009-08-13 23:36:34 +0000 | [diff] [blame] | 127 | default: assert(0 && "Invalid flag!"); |
Kevin Enderby | f96db46 | 2009-07-16 17:56:39 +0000 | [diff] [blame] | 128 | case SubsectionsViaSymbols: OS << ".subsections_via_symbols"; break; |
| 129 | } |
| 130 | OS << '\n'; |
Kevin Enderby | a5c7832 | 2009-07-13 21:03:15 +0000 | [diff] [blame] | 131 | } |
| 132 | |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 133 | void MCAsmStreamer::EmitAssignment(MCSymbol *Symbol, const MCValue &Value, |
| 134 | bool MakeAbsolute) { |
Daniel Dunbar | 71d259b | 2009-06-24 17:00:42 +0000 | [diff] [blame] | 135 | assert(!Symbol->getSection() && "Cannot assign to a label!"); |
| 136 | |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 137 | if (MakeAbsolute) { |
Daniel Dunbar | ad4555c | 2009-07-31 23:04:32 +0000 | [diff] [blame] | 138 | OS << ".set " << Symbol << ", " << Value << '\n'; |
Daniel Dunbar | b2d0b6b | 2009-08-14 19:10:46 +0000 | [diff] [blame^] | 139 | |
| 140 | // HACK: If the value isn't already absolute, set the symbol value to |
| 141 | // itself, we want to use the .set absolute value, not the actual |
| 142 | // expression. |
| 143 | if (!Value.isAbsolute()) |
| 144 | getContext().SetSymbolValue(Symbol, MCValue::get(Symbol)); |
| 145 | else |
| 146 | getContext().SetSymbolValue(Symbol, Value); |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 147 | } else { |
Daniel Dunbar | ad4555c | 2009-07-31 23:04:32 +0000 | [diff] [blame] | 148 | OS << Symbol << " = " << Value << '\n'; |
Daniel Dunbar | b2d0b6b | 2009-08-14 19:10:46 +0000 | [diff] [blame^] | 149 | getContext().SetSymbolValue(Symbol, Value); |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 150 | } |
| 151 | } |
| 152 | |
| 153 | void MCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol, |
| 154 | SymbolAttr Attribute) { |
| 155 | switch (Attribute) { |
| 156 | case Global: OS << ".globl"; break; |
Daniel Dunbar | d814b21 | 2009-06-24 16:36:52 +0000 | [diff] [blame] | 157 | case Hidden: OS << ".hidden"; break; |
| 158 | case IndirectSymbol: OS << ".indirect_symbol"; break; |
| 159 | case Internal: OS << ".internal"; break; |
| 160 | case LazyReference: OS << ".lazy_reference"; break; |
| 161 | case NoDeadStrip: OS << ".no_dead_strip"; break; |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 162 | case PrivateExtern: OS << ".private_extern"; break; |
Daniel Dunbar | d814b21 | 2009-06-24 16:36:52 +0000 | [diff] [blame] | 163 | case Protected: OS << ".protected"; break; |
| 164 | case Reference: OS << ".reference"; break; |
| 165 | case Weak: OS << ".weak"; break; |
| 166 | case WeakDefinition: OS << ".weak_definition"; break; |
| 167 | case WeakReference: OS << ".weak_reference"; break; |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 168 | } |
| 169 | |
Daniel Dunbar | ad4555c | 2009-07-31 23:04:32 +0000 | [diff] [blame] | 170 | OS << ' ' << Symbol << '\n'; |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 171 | } |
| 172 | |
Kevin Enderby | 95cf30c | 2009-07-14 18:17:10 +0000 | [diff] [blame] | 173 | void MCAsmStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) { |
Daniel Dunbar | ad4555c | 2009-07-31 23:04:32 +0000 | [diff] [blame] | 174 | OS << ".desc" << ' ' << Symbol << ',' << DescValue << '\n'; |
Kevin Enderby | 95cf30c | 2009-07-14 18:17:10 +0000 | [diff] [blame] | 175 | } |
| 176 | |
Kevin Enderby | 7114824 | 2009-07-14 21:35:03 +0000 | [diff] [blame] | 177 | void MCAsmStreamer::EmitLocalSymbol(MCSymbol *Symbol, const MCValue &Value) { |
Daniel Dunbar | ad4555c | 2009-07-31 23:04:32 +0000 | [diff] [blame] | 178 | OS << ".lsym" << ' ' << Symbol << ',' << Value << '\n'; |
Kevin Enderby | 7114824 | 2009-07-14 21:35:03 +0000 | [diff] [blame] | 179 | } |
| 180 | |
Chris Lattner | 4e4db7a | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 181 | void MCAsmStreamer::EmitCommonSymbol(MCSymbol *Symbol, unsigned Size, |
Chris Lattner | 1fc3d75 | 2009-07-09 17:25:12 +0000 | [diff] [blame] | 182 | unsigned Pow2Alignment, bool IsLocal) { |
| 183 | if (IsLocal) |
| 184 | OS << ".lcomm"; |
| 185 | else |
| 186 | OS << ".comm"; |
Daniel Dunbar | ad4555c | 2009-07-31 23:04:32 +0000 | [diff] [blame] | 187 | OS << ' ' << Symbol << ',' << Size; |
Chris Lattner | 4e4db7a | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 188 | if (Pow2Alignment != 0) |
| 189 | OS << ',' << Pow2Alignment; |
| 190 | OS << '\n'; |
| 191 | } |
| 192 | |
Chris Lattner | 9be3fee | 2009-07-10 22:20:30 +0000 | [diff] [blame] | 193 | void MCAsmStreamer::EmitZerofill(MCSection *Section, MCSymbol *Symbol, |
| 194 | unsigned Size, unsigned Pow2Alignment) { |
Daniel Dunbar | 011e4db | 2009-08-13 23:36:34 +0000 | [diff] [blame] | 195 | // Note: a .zerofill directive does not switch sections. |
Chris Lattner | 93b6db3 | 2009-08-08 23:39:42 +0000 | [diff] [blame] | 196 | OS << ".zerofill "; |
| 197 | |
| 198 | // This is a mach-o specific directive. |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 199 | const MCSectionMachO *MOSection = ((const MCSectionMachO*)Section); |
Daniel Dunbar | 12de0df | 2009-08-14 18:51:45 +0000 | [diff] [blame] | 200 | OS << MOSection->getSegmentName() << "," << MOSection->getSectionName(); |
Chris Lattner | 93b6db3 | 2009-08-08 23:39:42 +0000 | [diff] [blame] | 201 | |
Chris Lattner | 9be3fee | 2009-07-10 22:20:30 +0000 | [diff] [blame] | 202 | if (Symbol != NULL) { |
Daniel Dunbar | ad4555c | 2009-07-31 23:04:32 +0000 | [diff] [blame] | 203 | OS << ',' << Symbol << ',' << Size; |
Chris Lattner | 9be3fee | 2009-07-10 22:20:30 +0000 | [diff] [blame] | 204 | if (Pow2Alignment != 0) |
| 205 | OS << ',' << Pow2Alignment; |
| 206 | } |
| 207 | OS << '\n'; |
| 208 | } |
| 209 | |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 210 | void MCAsmStreamer::EmitBytes(const StringRef &Data) { |
Daniel Dunbar | 71d259b | 2009-06-24 17:00:42 +0000 | [diff] [blame] | 211 | assert(CurSection && "Cannot emit contents before setting section!"); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 212 | for (unsigned i = 0, e = Data.size(); i != e; ++i) |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 213 | OS << ".byte " << (unsigned) Data[i] << '\n'; |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | void MCAsmStreamer::EmitValue(const MCValue &Value, unsigned Size) { |
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. |
| 219 | switch (Size) { |
| 220 | default: |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 221 | llvm_unreachable("Invalid size for machine code value!"); |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 222 | case 1: OS << ".byte"; break; |
Daniel Dunbar | f5e75a1 | 2009-06-24 16:05:35 +0000 | [diff] [blame] | 223 | case 2: OS << ".short"; break; |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 224 | case 4: OS << ".long"; break; |
| 225 | case 8: OS << ".quad"; break; |
| 226 | } |
| 227 | |
Daniel Dunbar | 304f6a4 | 2009-06-25 21:03:18 +0000 | [diff] [blame] | 228 | OS << ' ' << truncateToSize(Value, Size) << '\n'; |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 229 | } |
| 230 | |
Daniel Dunbar | 84a2926 | 2009-06-24 19:25:34 +0000 | [diff] [blame] | 231 | void MCAsmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value, |
| 232 | unsigned ValueSize, |
| 233 | unsigned MaxBytesToEmit) { |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 234 | // Some assemblers don't support .balign, so we always emit as .p2align if |
| 235 | // this is a power of two. Otherwise we assume the client knows the target |
| 236 | // supports .balign and use that. |
Daniel Dunbar | 84a2926 | 2009-06-24 19:25:34 +0000 | [diff] [blame] | 237 | unsigned Pow2 = Log2_32(ByteAlignment); |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 238 | bool IsPow2 = (1U << Pow2) == ByteAlignment; |
Daniel Dunbar | 84a2926 | 2009-06-24 19:25:34 +0000 | [diff] [blame] | 239 | |
| 240 | switch (ValueSize) { |
| 241 | default: |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 242 | llvm_unreachable("Invalid size for machine code value!"); |
Daniel Dunbar | 84a2926 | 2009-06-24 19:25:34 +0000 | [diff] [blame] | 243 | case 8: |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 244 | llvm_unreachable("Unsupported alignment size!"); |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 245 | case 1: OS << (IsPow2 ? ".p2align" : ".balign"); break; |
| 246 | case 2: OS << (IsPow2 ? ".p2alignw" : ".balignw"); break; |
| 247 | case 4: OS << (IsPow2 ? ".p2alignl" : ".balignl"); break; |
Daniel Dunbar | 84a2926 | 2009-06-24 19:25:34 +0000 | [diff] [blame] | 248 | } |
| 249 | |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 250 | OS << ' ' << (IsPow2 ? Pow2 : ByteAlignment); |
Daniel Dunbar | 84a2926 | 2009-06-24 19:25:34 +0000 | [diff] [blame] | 251 | |
Daniel Dunbar | 304f6a4 | 2009-06-25 21:03:18 +0000 | [diff] [blame] | 252 | OS << ", " << truncateToSize(Value, ValueSize); |
Daniel Dunbar | 84a2926 | 2009-06-24 19:25:34 +0000 | [diff] [blame] | 253 | if (MaxBytesToEmit) |
| 254 | OS << ", " << MaxBytesToEmit; |
| 255 | OS << '\n'; |
| 256 | } |
| 257 | |
| 258 | void MCAsmStreamer::EmitValueToOffset(const MCValue &Offset, |
| 259 | unsigned char Value) { |
| 260 | // FIXME: Verify that Offset is associated with the current section. |
| 261 | OS << ".org " << Offset << ", " << (unsigned) Value << '\n'; |
| 262 | } |
| 263 | |
Daniel Dunbar | abde298 | 2009-07-01 06:35:03 +0000 | [diff] [blame] | 264 | static raw_ostream &operator<<(raw_ostream &OS, const MCOperand &Op) { |
| 265 | if (Op.isReg()) |
| 266 | return OS << "reg:" << Op.getReg(); |
| 267 | if (Op.isImm()) |
| 268 | return OS << "imm:" << Op.getImm(); |
| 269 | if (Op.isMBBLabel()) |
| 270 | return OS << "mbblabel:(" |
| 271 | << Op.getMBBLabelFunction() << ", " << Op.getMBBLabelBlock(); |
| 272 | assert(Op.isMCValue() && "Invalid operand!"); |
| 273 | return OS << "val:" << Op.getMCValue(); |
| 274 | } |
| 275 | |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 276 | void MCAsmStreamer::EmitInstruction(const MCInst &Inst) { |
Daniel Dunbar | 71d259b | 2009-06-24 17:00:42 +0000 | [diff] [blame] | 277 | assert(CurSection && "Cannot emit contents before setting section!"); |
Daniel Dunbar | c22e0b2 | 2009-08-14 03:48:55 +0000 | [diff] [blame] | 278 | |
| 279 | // If we have an AsmPrinter, use that to print. |
| 280 | if (Printer) { |
| 281 | Printer->printMCInst(&Inst); |
| 282 | return; |
| 283 | } |
| 284 | |
| 285 | // Otherwise fall back to a structural printing for now. Eventually we should |
| 286 | // always have access to the target specific printer. |
Daniel Dunbar | abde298 | 2009-07-01 06:35:03 +0000 | [diff] [blame] | 287 | OS << "MCInst(" |
| 288 | << "opcode=" << Inst.getOpcode() << ", " |
| 289 | << "operands=["; |
| 290 | for (unsigned i = 0, e = Inst.getNumOperands(); i != e; ++i) { |
| 291 | if (i) |
| 292 | OS << ", "; |
| 293 | OS << Inst.getOperand(i); |
| 294 | } |
| 295 | OS << "])\n"; |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | void MCAsmStreamer::Finish() { |
| 299 | OS.flush(); |
| 300 | } |
| 301 | |
Daniel Dunbar | c22e0b2 | 2009-08-14 03:48:55 +0000 | [diff] [blame] | 302 | MCStreamer *llvm::createAsmStreamer(MCContext &Context, raw_ostream &OS, |
| 303 | AsmPrinter *AP) { |
| 304 | return new MCAsmStreamer(Context, OS, AP); |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 305 | } |