assert(0) -> LLVM_UNREACHABLE.
[oota-llvm.git] / lib / MC / MCAsmStreamer.cpp
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
12 #include "llvm/MC/MCContext.h"
13 #include "llvm/MC/MCInst.h"
14 #include "llvm/MC/MCSection.h"
15 #include "llvm/MC/MCSymbol.h"
16 #include "llvm/MC/MCValue.h"
17 #include "llvm/Support/ErrorHandling.h"
18 #include "llvm/Support/raw_ostream.h"
19
20 using namespace llvm;
21
22 namespace {
23
24   class MCAsmStreamer : public MCStreamer {
25     raw_ostream &OS;
26
27     MCSection *CurSection;
28
29   public:
30     MCAsmStreamer(MCContext &Context, raw_ostream &_OS)
31       : MCStreamer(Context), OS(_OS), CurSection(0) {}
32     ~MCAsmStreamer() {}
33
34     /// @name MCStreamer Interface
35     /// @{
36
37     virtual void SwitchSection(MCSection *Section);
38
39     virtual void EmitLabel(MCSymbol *Symbol);
40
41     virtual void EmitAssignment(MCSymbol *Symbol, const MCValue &Value,
42                                 bool MakeAbsolute = false);
43
44     virtual void EmitSymbolAttribute(MCSymbol *Symbol, SymbolAttr Attribute);
45
46     virtual void EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
47                                   unsigned Pow2Alignment, bool IsLocal);
48
49     virtual void EmitZerofill(MCSection *Section, MCSymbol *Symbol = NULL,
50                               unsigned Size = 0, unsigned Pow2Alignment = 0);
51
52     virtual void EmitBytes(const char *Data, unsigned Length);
53
54     virtual void EmitValue(const MCValue &Value, unsigned Size);
55
56     virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
57                                       unsigned ValueSize = 1,
58                                       unsigned MaxBytesToEmit = 0);
59
60     virtual void EmitValueToOffset(const MCValue &Offset, 
61                                    unsigned char Value = 0);
62     
63     virtual void EmitInstruction(const MCInst &Inst);
64
65     virtual void Finish();
66     
67     /// @}
68   };
69
70 }
71
72 /// Allow printing values directly to a raw_ostream.
73 static inline raw_ostream &operator<<(raw_ostream &os, const MCValue &Value) {
74   if (Value.getSymA()) {
75     os << Value.getSymA()->getName();
76     if (Value.getSymB())
77       os << " - " << Value.getSymB()->getName();
78     if (Value.getConstant())
79       os << " + " << Value.getConstant();
80   } else {
81     assert(!Value.getSymB() && "Invalid machine code value!");
82     os << Value.getConstant();
83   }
84
85   return os;
86 }
87
88 static inline int64_t truncateToSize(int64_t Value, unsigned Bytes) {
89   assert(Bytes && "Invalid size!");
90   return Value & ((uint64_t) (int64_t) -1 >> (64 - Bytes * 8));
91 }
92
93 static inline MCValue truncateToSize(const MCValue &Value, unsigned Bytes) {
94   return MCValue::get(Value.getSymA(), Value.getSymB(), 
95                       truncateToSize(Value.getConstant(), Bytes));
96 }
97
98 void MCAsmStreamer::SwitchSection(MCSection *Section) {
99   if (Section != CurSection) {
100     CurSection = Section;
101
102     // FIXME: Really we would like the segment, flags, etc. to be separate
103     // values instead of embedded in the name. Not all assemblers understand all
104     // this stuff though.
105     OS << ".section " << Section->getName() << "\n";
106   }
107 }
108
109 void MCAsmStreamer::EmitLabel(MCSymbol *Symbol) {
110   assert(Symbol->getSection() == 0 && "Cannot emit a symbol twice!");
111   assert(CurSection && "Cannot emit before setting section!");
112   assert(!getContext().GetSymbolValue(Symbol) && 
113          "Cannot emit symbol which was directly assigned to!");
114
115   OS << Symbol->getName() << ":\n";
116   Symbol->setSection(CurSection);
117   Symbol->setExternal(false);
118 }
119
120 void MCAsmStreamer::EmitAssignment(MCSymbol *Symbol, const MCValue &Value,
121                                    bool MakeAbsolute) {
122   assert(!Symbol->getSection() && "Cannot assign to a label!");
123
124   if (MakeAbsolute) {
125     OS << ".set " << Symbol->getName() << ", " << Value << '\n';
126   } else {
127     OS << Symbol->getName() << " = " << Value << '\n';
128   }
129
130   getContext().SetSymbolValue(Symbol, Value);
131 }
132
133 void MCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol, 
134                                         SymbolAttr Attribute) {
135   switch (Attribute) {
136   case Global: OS << ".globl"; break;
137   case Hidden: OS << ".hidden"; break;
138   case IndirectSymbol: OS << ".indirect_symbol"; break;
139   case Internal: OS << ".internal"; break;
140   case LazyReference: OS << ".lazy_reference"; break;
141   case NoDeadStrip: OS << ".no_dead_strip"; break;
142   case PrivateExtern: OS << ".private_extern"; break;
143   case Protected: OS << ".protected"; break;
144   case Reference: OS << ".reference"; break;
145   case Weak: OS << ".weak"; break;
146   case WeakDefinition: OS << ".weak_definition"; break;
147   case WeakReference: OS << ".weak_reference"; break;
148   }
149
150   OS << ' ' << Symbol->getName() << '\n';
151 }
152
153 void MCAsmStreamer::EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
154                                      unsigned Pow2Alignment, bool IsLocal) {
155   if (IsLocal)
156     OS << ".lcomm";
157   else
158     OS << ".comm";
159   OS << ' ' << Symbol->getName() << ',' << Size;
160   if (Pow2Alignment != 0)
161     OS << ',' << Pow2Alignment;
162   OS << '\n';
163 }
164
165 void MCAsmStreamer::EmitZerofill(MCSection *Section, MCSymbol *Symbol,
166                                  unsigned Size, unsigned Pow2Alignment) {
167   // Note: a .zerofill directive does not switch sections
168   // FIXME: Really we would like the segment and section names as well as the
169   // section type to be separate values instead of embedded in the name. Not
170   // all assemblers understand all this stuff though.
171   OS << ".zerofill " << Section->getName();
172   if (Symbol != NULL) {
173     OS << ',' << Symbol->getName() << ',' << Size;
174     if (Pow2Alignment != 0)
175       OS << ',' << Pow2Alignment;
176   }
177   OS << '\n';
178 }
179
180 void MCAsmStreamer::EmitBytes(const char *Data, unsigned Length) {
181   assert(CurSection && "Cannot emit contents before setting section!");
182   for (unsigned i = 0; i != Length; ++i)
183     OS << ".byte " << (unsigned) Data[i] << '\n';
184 }
185
186 void MCAsmStreamer::EmitValue(const MCValue &Value, unsigned Size) {
187   assert(CurSection && "Cannot emit contents before setting section!");
188   // Need target hooks to know how to print this.
189   switch (Size) {
190   default:
191     LLVM_UNREACHABLE("Invalid size for machine code value!");
192   case 1: OS << ".byte"; break;
193   case 2: OS << ".short"; break;
194   case 4: OS << ".long"; break;
195   case 8: OS << ".quad"; break;
196   }
197
198   OS << ' ' << truncateToSize(Value, Size) << '\n';
199 }
200
201 void MCAsmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
202                                          unsigned ValueSize,
203                                          unsigned MaxBytesToEmit) {
204   // Some assemblers don't support .balign, so we always emit as .p2align if
205   // this is a power of two. Otherwise we assume the client knows the target
206   // supports .balign and use that.
207   unsigned Pow2 = Log2_32(ByteAlignment);
208   bool IsPow2 = (1U << Pow2) == ByteAlignment;
209
210   switch (ValueSize) {
211   default:
212     LLVM_UNREACHABLE("Invalid size for machine code value!");
213   case 8:
214     LLVM_UNREACHABLE("Unsupported alignment size!");
215   case 1: OS << (IsPow2 ? ".p2align" : ".balign"); break;
216   case 2: OS << (IsPow2 ? ".p2alignw" : ".balignw"); break;
217   case 4: OS << (IsPow2 ? ".p2alignl" : ".balignl"); break;
218   }
219
220   OS << ' ' << (IsPow2 ? Pow2 : ByteAlignment);
221
222   OS << ", " << truncateToSize(Value, ValueSize);
223   if (MaxBytesToEmit) 
224     OS << ", " << MaxBytesToEmit;
225   OS << '\n';
226 }
227
228 void MCAsmStreamer::EmitValueToOffset(const MCValue &Offset, 
229                                       unsigned char Value) {
230   // FIXME: Verify that Offset is associated with the current section.
231   OS << ".org " << Offset << ", " << (unsigned) Value << '\n';
232 }
233
234 static raw_ostream &operator<<(raw_ostream &OS, const MCOperand &Op) {
235   if (Op.isReg())
236     return OS << "reg:" << Op.getReg();
237   if (Op.isImm())
238     return OS << "imm:" << Op.getImm();
239   if (Op.isMBBLabel())
240     return OS << "mbblabel:(" 
241               << Op.getMBBLabelFunction() << ", " << Op.getMBBLabelBlock();
242   assert(Op.isMCValue() && "Invalid operand!");
243   return OS << "val:" << Op.getMCValue();
244 }
245
246 void MCAsmStreamer::EmitInstruction(const MCInst &Inst) {
247   assert(CurSection && "Cannot emit contents before setting section!");
248   // FIXME: Implement proper printing.
249   OS << "MCInst("
250      << "opcode=" << Inst.getOpcode() << ", "
251      << "operands=[";
252   for (unsigned i = 0, e = Inst.getNumOperands(); i != e; ++i) {
253     if (i)
254       OS << ", ";
255     OS << Inst.getOperand(i);
256   }
257   OS << "])\n";
258 }
259
260 void MCAsmStreamer::Finish() {
261   OS.flush();
262 }
263     
264 MCStreamer *llvm::createAsmStreamer(MCContext &Context, raw_ostream &OS) {
265   return new MCAsmStreamer(Context, OS);
266 }