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