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 | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 12 | #include "llvm/MC/MCContext.h" |
| 13 | #include "llvm/MC/MCSection.h" |
| 14 | #include "llvm/MC/MCSymbol.h" |
| 15 | #include "llvm/MC/MCValue.h" |
| 16 | #include "llvm/Support/raw_ostream.h" |
| 17 | using namespace llvm; |
| 18 | |
| 19 | namespace { |
| 20 | |
| 21 | class MCAsmStreamer : public MCStreamer { |
| 22 | raw_ostream &OS; |
| 23 | |
| 24 | MCSection *CurSection; |
| 25 | |
| 26 | public: |
| 27 | MCAsmStreamer(MCContext &Context, raw_ostream &_OS) |
Torok Edwin | 48de5d8 | 2009-06-29 19:59:10 +0000 | [diff] [blame^] | 28 | : MCStreamer(Context), OS(_OS), CurSection(0) {} |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 29 | ~MCAsmStreamer() {} |
| 30 | |
| 31 | /// @name MCStreamer Interface |
| 32 | /// @{ |
| 33 | |
| 34 | virtual void SwitchSection(MCSection *Section); |
| 35 | |
| 36 | virtual void EmitLabel(MCSymbol *Symbol); |
| 37 | |
| 38 | virtual void EmitAssignment(MCSymbol *Symbol, const MCValue &Value, |
| 39 | bool MakeAbsolute = false); |
| 40 | |
| 41 | virtual void EmitSymbolAttribute(MCSymbol *Symbol, SymbolAttr Attribute); |
| 42 | |
| 43 | virtual void EmitBytes(const char *Data, unsigned Length); |
| 44 | |
| 45 | virtual void EmitValue(const MCValue &Value, unsigned Size); |
| 46 | |
Daniel Dunbar | 84a2926 | 2009-06-24 19:25:34 +0000 | [diff] [blame] | 47 | virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0, |
| 48 | unsigned ValueSize = 1, |
| 49 | unsigned MaxBytesToEmit = 0); |
| 50 | |
| 51 | virtual void EmitValueToOffset(const MCValue &Offset, |
| 52 | unsigned char Value = 0); |
| 53 | |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 54 | virtual void EmitInstruction(const MCInst &Inst); |
| 55 | |
| 56 | virtual void Finish(); |
| 57 | |
| 58 | /// @} |
| 59 | }; |
| 60 | |
| 61 | } |
| 62 | |
| 63 | /// Allow printing values directly to a raw_ostream. |
Daniel Dunbar | 304f6a4 | 2009-06-25 21:03:18 +0000 | [diff] [blame] | 64 | static inline raw_ostream &operator<<(raw_ostream &os, const MCValue &Value) { |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 65 | if (Value.getSymA()) { |
| 66 | os << Value.getSymA()->getName(); |
| 67 | if (Value.getSymB()) |
| 68 | os << " - " << Value.getSymB()->getName(); |
Daniel Dunbar | 7908a6a | 2009-06-29 19:51:00 +0000 | [diff] [blame] | 69 | if (Value.getConstant()) |
| 70 | os << " + " << Value.getConstant(); |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 71 | } else { |
| 72 | assert(!Value.getSymB() && "Invalid machine code value!"); |
Daniel Dunbar | 7908a6a | 2009-06-29 19:51:00 +0000 | [diff] [blame] | 73 | os << Value.getConstant(); |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | return os; |
| 77 | } |
| 78 | |
Daniel Dunbar | 304f6a4 | 2009-06-25 21:03:18 +0000 | [diff] [blame] | 79 | static inline int64_t truncateToSize(int64_t Value, unsigned Bytes) { |
| 80 | assert(Bytes && "Invalid size!"); |
| 81 | return Value & ((uint64_t) (int64_t) -1 >> (64 - Bytes * 8)); |
| 82 | } |
| 83 | |
| 84 | static inline MCValue truncateToSize(const MCValue &Value, unsigned Bytes) { |
| 85 | return MCValue::get(Value.getSymA(), Value.getSymB(), |
Daniel Dunbar | 7908a6a | 2009-06-29 19:51:00 +0000 | [diff] [blame] | 86 | truncateToSize(Value.getConstant(), Bytes)); |
Daniel Dunbar | 304f6a4 | 2009-06-25 21:03:18 +0000 | [diff] [blame] | 87 | } |
| 88 | |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 89 | void MCAsmStreamer::SwitchSection(MCSection *Section) { |
| 90 | if (Section != CurSection) { |
| 91 | CurSection = Section; |
| 92 | |
| 93 | // FIXME: Really we would like the segment, flags, etc. to be separate |
| 94 | // values instead of embedded in the name. Not all assemblers understand all |
| 95 | // this stuff though. |
| 96 | OS << ".section " << Section->getName() << "\n"; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | void MCAsmStreamer::EmitLabel(MCSymbol *Symbol) { |
Daniel Dunbar | 71d259b | 2009-06-24 17:00:42 +0000 | [diff] [blame] | 101 | assert(Symbol->getSection() == 0 && "Cannot emit a symbol twice!"); |
| 102 | assert(CurSection && "Cannot emit before setting section!"); |
| 103 | assert(!getContext().GetSymbolValue(Symbol) && |
| 104 | "Cannot emit symbol which was directly assigned to!"); |
| 105 | |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 106 | OS << Symbol->getName() << ":\n"; |
Daniel Dunbar | 71d259b | 2009-06-24 17:00:42 +0000 | [diff] [blame] | 107 | Symbol->setSection(CurSection); |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | void MCAsmStreamer::EmitAssignment(MCSymbol *Symbol, const MCValue &Value, |
| 111 | bool MakeAbsolute) { |
Daniel Dunbar | 71d259b | 2009-06-24 17:00:42 +0000 | [diff] [blame] | 112 | assert(!Symbol->getSection() && "Cannot assign to a label!"); |
| 113 | |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 114 | if (MakeAbsolute) { |
| 115 | OS << ".set " << Symbol->getName() << ", " << Value << '\n'; |
| 116 | } else { |
| 117 | OS << Symbol->getName() << " = " << Value << '\n'; |
| 118 | } |
Daniel Dunbar | 71d259b | 2009-06-24 17:00:42 +0000 | [diff] [blame] | 119 | |
| 120 | getContext().SetSymbolValue(Symbol, Value); |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | void MCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol, |
| 124 | SymbolAttr Attribute) { |
| 125 | switch (Attribute) { |
| 126 | case Global: OS << ".globl"; break; |
Daniel Dunbar | d814b21 | 2009-06-24 16:36:52 +0000 | [diff] [blame] | 127 | case Hidden: OS << ".hidden"; break; |
| 128 | case IndirectSymbol: OS << ".indirect_symbol"; break; |
| 129 | case Internal: OS << ".internal"; break; |
| 130 | case LazyReference: OS << ".lazy_reference"; break; |
| 131 | case NoDeadStrip: OS << ".no_dead_strip"; break; |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 132 | case PrivateExtern: OS << ".private_extern"; break; |
Daniel Dunbar | d814b21 | 2009-06-24 16:36:52 +0000 | [diff] [blame] | 133 | case Protected: OS << ".protected"; break; |
| 134 | case Reference: OS << ".reference"; break; |
| 135 | case Weak: OS << ".weak"; break; |
| 136 | case WeakDefinition: OS << ".weak_definition"; break; |
| 137 | case WeakReference: OS << ".weak_reference"; break; |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | OS << ' ' << Symbol->getName() << '\n'; |
| 141 | } |
| 142 | |
| 143 | void MCAsmStreamer::EmitBytes(const char *Data, unsigned Length) { |
Daniel Dunbar | 71d259b | 2009-06-24 17:00:42 +0000 | [diff] [blame] | 144 | assert(CurSection && "Cannot emit contents before setting section!"); |
| 145 | for (unsigned i = 0; i != Length; ++i) |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 146 | OS << ".byte " << (unsigned) Data[i] << '\n'; |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | void MCAsmStreamer::EmitValue(const MCValue &Value, unsigned Size) { |
Daniel Dunbar | 71d259b | 2009-06-24 17:00:42 +0000 | [diff] [blame] | 150 | assert(CurSection && "Cannot emit contents before setting section!"); |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 151 | // Need target hooks to know how to print this. |
| 152 | switch (Size) { |
| 153 | default: |
| 154 | assert(0 && "Invalid size for machine code value!"); |
| 155 | case 1: OS << ".byte"; break; |
Daniel Dunbar | f5e75a1 | 2009-06-24 16:05:35 +0000 | [diff] [blame] | 156 | case 2: OS << ".short"; break; |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 157 | case 4: OS << ".long"; break; |
| 158 | case 8: OS << ".quad"; break; |
| 159 | } |
| 160 | |
Daniel Dunbar | 304f6a4 | 2009-06-25 21:03:18 +0000 | [diff] [blame] | 161 | OS << ' ' << truncateToSize(Value, Size) << '\n'; |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 162 | } |
| 163 | |
Daniel Dunbar | 84a2926 | 2009-06-24 19:25:34 +0000 | [diff] [blame] | 164 | void MCAsmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value, |
| 165 | unsigned ValueSize, |
| 166 | unsigned MaxBytesToEmit) { |
| 167 | unsigned Pow2 = Log2_32(ByteAlignment); |
| 168 | assert((1U << Pow2) == ByteAlignment && "Invalid alignment!"); |
| 169 | |
| 170 | switch (ValueSize) { |
| 171 | default: |
| 172 | assert(0 && "Invalid size for machine code value!"); |
| 173 | case 8: |
| 174 | assert(0 && "Unsupported alignment size!"); |
| 175 | case 1: OS << ".p2align"; break; |
| 176 | case 2: OS << ".p2alignw"; break; |
| 177 | case 4: OS << ".p2alignl"; break; |
| 178 | } |
| 179 | |
| 180 | OS << ' ' << Pow2; |
| 181 | |
Daniel Dunbar | 304f6a4 | 2009-06-25 21:03:18 +0000 | [diff] [blame] | 182 | OS << ", " << truncateToSize(Value, ValueSize); |
Daniel Dunbar | 84a2926 | 2009-06-24 19:25:34 +0000 | [diff] [blame] | 183 | if (MaxBytesToEmit) |
| 184 | OS << ", " << MaxBytesToEmit; |
| 185 | OS << '\n'; |
| 186 | } |
| 187 | |
| 188 | void MCAsmStreamer::EmitValueToOffset(const MCValue &Offset, |
| 189 | unsigned char Value) { |
| 190 | // FIXME: Verify that Offset is associated with the current section. |
| 191 | OS << ".org " << Offset << ", " << (unsigned) Value << '\n'; |
| 192 | } |
| 193 | |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 194 | void MCAsmStreamer::EmitInstruction(const MCInst &Inst) { |
Daniel Dunbar | 71d259b | 2009-06-24 17:00:42 +0000 | [diff] [blame] | 195 | assert(CurSection && "Cannot emit contents before setting section!"); |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 196 | // FIXME: Implement. |
| 197 | OS << "# FIXME: Implement instruction printing!\n"; |
| 198 | } |
| 199 | |
| 200 | void MCAsmStreamer::Finish() { |
| 201 | OS.flush(); |
| 202 | } |
| 203 | |
| 204 | MCStreamer *llvm::createAsmStreamer(MCContext &Context, raw_ostream &OS) { |
| 205 | return new MCAsmStreamer(Context, OS); |
| 206 | } |