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