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