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