89bc5bc637a5b63841590f0aeb23473c1fda3162
[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 #include "llvm/CodeGen/AsmPrinter.h"
12 #include "llvm/MC/MCContext.h"
13 #include "llvm/MC/MCInst.h"
14 #include "llvm/MC/MCSectionMachO.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 using namespace llvm;
20
21 namespace {
22
23 class MCAsmStreamer : public MCStreamer {
24   raw_ostream &OS;
25   const TargetAsmInfo &TAI;
26   AsmPrinter *Printer;
27 public:
28   MCAsmStreamer(MCContext &Context, raw_ostream &_OS, const TargetAsmInfo &tai,
29                 AsmPrinter *_AsmPrinter)
30     : MCStreamer(Context), OS(_OS), TAI(tai), Printer(_AsmPrinter) {}
31   ~MCAsmStreamer() {}
32
33   /// @name MCStreamer Interface
34   /// @{
35
36   virtual void SwitchSection(const MCSection *Section);
37
38   virtual void EmitLabel(MCSymbol *Symbol);
39
40   virtual void EmitAssemblerFlag(AssemblerFlag Flag);
41
42   virtual void EmitAssignment(MCSymbol *Symbol, const MCValue &Value,
43                               bool MakeAbsolute = false);
44
45   virtual void EmitSymbolAttribute(MCSymbol *Symbol, SymbolAttr Attribute);
46
47   virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
48
49   virtual void EmitLocalSymbol(MCSymbol *Symbol, const MCValue &Value);
50
51   virtual void EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
52                                 unsigned Pow2Alignment, bool IsLocal);
53
54   virtual void EmitZerofill(MCSection *Section, MCSymbol *Symbol = NULL,
55                             unsigned Size = 0, unsigned Pow2Alignment = 0);
56
57   virtual void EmitBytes(const StringRef &Data);
58
59   virtual void EmitValue(const MCValue &Value, unsigned Size);
60
61   virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
62                                     unsigned ValueSize = 1,
63                                     unsigned MaxBytesToEmit = 0);
64
65   virtual void EmitValueToOffset(const MCValue &Offset, 
66                                  unsigned char Value = 0);
67   
68   virtual void EmitInstruction(const MCInst &Inst);
69
70   virtual void Finish();
71   
72   /// @}
73 };
74
75 } // end anonymous namespace.
76
77 /// Allow printing symbols directly to a raw_ostream with proper quoting.
78 static inline raw_ostream &operator<<(raw_ostream &os, const MCSymbol *S) {
79   S->print(os);
80   return os;
81 }
82
83 /// Allow printing values directly to a raw_ostream.
84 static inline raw_ostream &operator<<(raw_ostream &os, const MCValue &Value) {
85   Value.print(os);
86   return os;
87 }
88
89 static inline int64_t truncateToSize(int64_t Value, unsigned Bytes) {
90   assert(Bytes && "Invalid size!");
91   return Value & ((uint64_t) (int64_t) -1 >> (64 - Bytes * 8));
92 }
93
94 static inline MCValue truncateToSize(const MCValue &Value, unsigned Bytes) {
95   return MCValue::get(Value.getSymA(), Value.getSymB(), 
96                       truncateToSize(Value.getConstant(), Bytes));
97 }
98
99 void MCAsmStreamer::SwitchSection(const MCSection *Section) {
100   assert(Section && "Cannot switch to a null section!");
101   if (Section != CurSection) {
102     CurSection = Section;
103     Section->PrintSwitchToSection(TAI, OS);
104   }
105 }
106
107 void MCAsmStreamer::EmitLabel(MCSymbol *Symbol) {
108   assert(Symbol->getSection() == 0 && "Cannot emit a symbol twice!");
109   assert(CurSection && "Cannot emit before setting section!");
110   assert(!getContext().GetSymbolValue(Symbol) && 
111          "Cannot emit symbol which was directly assigned to!");
112
113   OS << Symbol << ":\n";
114   Symbol->setSection(CurSection);
115   Symbol->setExternal(false);
116 }
117
118 void MCAsmStreamer::EmitAssemblerFlag(AssemblerFlag Flag) {
119   switch (Flag) {
120   default: assert(0 && "Invalid flag!");
121   case SubsectionsViaSymbols: OS << ".subsections_via_symbols"; break;
122   }
123   OS << '\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 << ", " << Value << '\n';
132
133     // HACK: If the value isn't already absolute, set the symbol value to
134     // itself, we want to use the .set absolute value, not the actual
135     // expression.
136     if (!Value.isAbsolute())
137       getContext().SetSymbolValue(Symbol, MCValue::get(Symbol));
138     else
139       getContext().SetSymbolValue(Symbol, Value);
140   } else {
141     OS << Symbol << " = " << Value << '\n';
142     getContext().SetSymbolValue(Symbol, Value);
143   }
144 }
145
146 void MCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol, 
147                                         SymbolAttr Attribute) {
148   switch (Attribute) {
149   case Global: OS << ".globl"; break;
150   case Hidden: OS << ".hidden"; break;
151   case IndirectSymbol: OS << ".indirect_symbol"; break;
152   case Internal: OS << ".internal"; break;
153   case LazyReference: OS << ".lazy_reference"; break;
154   case NoDeadStrip: OS << ".no_dead_strip"; break;
155   case PrivateExtern: OS << ".private_extern"; break;
156   case Protected: OS << ".protected"; break;
157   case Reference: OS << ".reference"; break;
158   case Weak: OS << ".weak"; break;
159   case WeakDefinition: OS << ".weak_definition"; break;
160   case WeakReference: OS << ".weak_reference"; break;
161   }
162
163   OS << ' ' << Symbol << '\n';
164 }
165
166 void MCAsmStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
167   OS << ".desc" << ' ' << Symbol << ',' << DescValue << '\n';
168 }
169
170 void MCAsmStreamer::EmitLocalSymbol(MCSymbol *Symbol, const MCValue &Value) {
171   OS << ".lsym" << ' ' << Symbol << ',' << Value << '\n';
172 }
173
174 void MCAsmStreamer::EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
175                                      unsigned Pow2Alignment, bool IsLocal) {
176   if (IsLocal)
177     OS << ".lcomm";
178   else
179     OS << ".comm";
180   OS << ' ' << Symbol << ',' << Size;
181   if (Pow2Alignment != 0)
182     OS << ',' << Pow2Alignment;
183   OS << '\n';
184 }
185
186 void MCAsmStreamer::EmitZerofill(MCSection *Section, MCSymbol *Symbol,
187                                  unsigned Size, unsigned Pow2Alignment) {
188   // Note: a .zerofill directive does not switch sections.
189   OS << ".zerofill ";
190   
191   // This is a mach-o specific directive.
192   const MCSectionMachO *MOSection = ((const MCSectionMachO*)Section);
193   OS << MOSection->getSegmentName() << "," << MOSection->getSectionName();
194   
195   if (Symbol != NULL) {
196     OS << ',' << Symbol << ',' << Size;
197     if (Pow2Alignment != 0)
198       OS << ',' << Pow2Alignment;
199   }
200   OS << '\n';
201 }
202
203 void MCAsmStreamer::EmitBytes(const StringRef &Data) {
204   assert(CurSection && "Cannot emit contents before setting section!");
205   for (unsigned i = 0, e = Data.size(); i != e; ++i)
206     OS << ".byte " << (unsigned) (unsigned char) Data[i] << '\n';
207 }
208
209 void MCAsmStreamer::EmitValue(const MCValue &Value, unsigned Size) {
210   assert(CurSection && "Cannot emit contents before setting section!");
211   // Need target hooks to know how to print this.
212   switch (Size) {
213   default:
214     llvm_unreachable("Invalid size for machine code value!");
215   case 1: OS << ".byte"; break;
216   case 2: OS << ".short"; break;
217   case 4: OS << ".long"; break;
218   case 8: OS << ".quad"; break;
219   }
220
221   OS << ' ' << truncateToSize(Value, Size) << '\n';
222 }
223
224 void MCAsmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
225                                          unsigned ValueSize,
226                                          unsigned MaxBytesToEmit) {
227   // Some assemblers don't support .balign, so we always emit as .p2align if
228   // this is a power of two. Otherwise we assume the client knows the target
229   // supports .balign and use that.
230   unsigned Pow2 = Log2_32(ByteAlignment);
231   bool IsPow2 = (1U << Pow2) == ByteAlignment;
232
233   switch (ValueSize) {
234   default:
235     llvm_unreachable("Invalid size for machine code value!");
236   case 8:
237     llvm_unreachable("Unsupported alignment size!");
238   case 1: OS << (IsPow2 ? ".p2align" : ".balign"); break;
239   case 2: OS << (IsPow2 ? ".p2alignw" : ".balignw"); break;
240   case 4: OS << (IsPow2 ? ".p2alignl" : ".balignl"); break;
241   }
242
243   OS << ' ' << (IsPow2 ? Pow2 : ByteAlignment);
244
245   OS << ", " << truncateToSize(Value, ValueSize);
246   if (MaxBytesToEmit) 
247     OS << ", " << MaxBytesToEmit;
248   OS << '\n';
249 }
250
251 void MCAsmStreamer::EmitValueToOffset(const MCValue &Offset, 
252                                       unsigned char Value) {
253   // FIXME: Verify that Offset is associated with the current section.
254   OS << ".org " << Offset << ", " << (unsigned) Value << '\n';
255 }
256
257 static raw_ostream &operator<<(raw_ostream &OS, const MCOperand &Op) {
258   if (Op.isReg())
259     return OS << "reg:" << Op.getReg();
260   if (Op.isImm())
261     return OS << "imm:" << Op.getImm();
262   if (Op.isMBBLabel())
263     return OS << "mbblabel:(" 
264               << Op.getMBBLabelFunction() << ", " << Op.getMBBLabelBlock();
265   assert(Op.isMCValue() && "Invalid operand!");
266   return OS << "val:" << Op.getMCValue();
267 }
268
269 void MCAsmStreamer::EmitInstruction(const MCInst &Inst) {
270   assert(CurSection && "Cannot emit contents before setting section!");
271
272   // If we have an AsmPrinter, use that to print.
273   if (Printer) {
274     Printer->printMCInst(&Inst);
275     return;
276   }
277
278   // Otherwise fall back to a structural printing for now. Eventually we should
279   // always have access to the target specific printer.
280   OS << "MCInst("
281      << "opcode=" << Inst.getOpcode() << ", "
282      << "operands=[";
283   for (unsigned i = 0, e = Inst.getNumOperands(); i != e; ++i) {
284     if (i)
285       OS << ", ";
286     OS << Inst.getOperand(i);
287   }
288   OS << "])\n";
289 }
290
291 void MCAsmStreamer::Finish() {
292   OS.flush();
293 }
294     
295 MCStreamer *llvm::createAsmStreamer(MCContext &Context, raw_ostream &OS,
296                                     const TargetAsmInfo &TAI, AsmPrinter *AP) {
297   return new MCAsmStreamer(Context, OS, TAI, AP);
298 }