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