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