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