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