a126b52786315cf4b96a4f5694fad9ac9cb4c922
[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/ADT/SmallString.h"
12 #include "llvm/MC/MCAsmInfo.h"
13 #include "llvm/MC/MCCodeEmitter.h"
14 #include "llvm/MC/MCContext.h"
15 #include "llvm/MC/MCExpr.h"
16 #include "llvm/MC/MCInst.h"
17 #include "llvm/MC/MCInstPrinter.h"
18 #include "llvm/MC/MCSectionMachO.h"
19 #include "llvm/MC/MCSymbol.h"
20 #include "llvm/Support/ErrorHandling.h"
21 #include "llvm/Support/MathExtras.h"
22 #include "llvm/Support/Format.h"
23 #include "llvm/Support/raw_ostream.h"
24 using namespace llvm;
25
26 namespace {
27
28 class MCAsmStreamer : public MCStreamer {
29   raw_ostream &OS;
30   const MCAsmInfo &MAI;
31   MCInstPrinter *InstPrinter;
32   MCCodeEmitter *Emitter;
33 public:
34   MCAsmStreamer(MCContext &Context, raw_ostream &_OS, const MCAsmInfo &tai,
35                 MCInstPrinter *_Printer, MCCodeEmitter *_Emitter)
36     : MCStreamer(Context), OS(_OS), MAI(tai), InstPrinter(_Printer),
37       Emitter(_Emitter) {}
38   ~MCAsmStreamer() {}
39
40   /// @name MCStreamer Interface
41   /// @{
42
43   virtual void SwitchSection(const MCSection *Section);
44
45   virtual void EmitLabel(MCSymbol *Symbol);
46
47   virtual void EmitAssemblerFlag(AssemblerFlag Flag);
48
49   virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value);
50
51   virtual void EmitSymbolAttribute(MCSymbol *Symbol, SymbolAttr Attribute);
52
53   virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
54
55   virtual void EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
56                                 unsigned ByteAlignment);
57
58   virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
59                             unsigned Size = 0, unsigned ByteAlignment = 0);
60
61   virtual void EmitBytes(StringRef Data, unsigned AddrSpace);
62
63   virtual void EmitValue(const MCExpr *Value, unsigned Size,unsigned AddrSpace);
64   virtual void EmitIntValue(uint64_t Value, unsigned Size, unsigned AddrSpace);
65
66   virtual void EmitFill(uint64_t NumBytes, uint8_t FillValue,
67                         unsigned AddrSpace);
68
69   virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
70                                     unsigned ValueSize = 1,
71                                     unsigned MaxBytesToEmit = 0);
72
73   virtual void EmitValueToOffset(const MCExpr *Offset,
74                                  unsigned char Value = 0);
75   
76   virtual void EmitInstruction(const MCInst &Inst);
77
78   virtual void Finish();
79   
80   /// @}
81 };
82
83 } // end anonymous namespace.
84
85 static inline int64_t truncateToSize(int64_t Value, unsigned Bytes) {
86   assert(Bytes && "Invalid size!");
87   return Value & ((uint64_t) (int64_t) -1 >> (64 - Bytes * 8));
88 }
89
90 static inline const MCExpr *truncateToSize(const MCExpr *Value,
91                                            unsigned Bytes) {
92   // FIXME: Do we really need this routine?
93   return Value;
94 }
95
96 void MCAsmStreamer::SwitchSection(const MCSection *Section) {
97   assert(Section && "Cannot switch to a null section!");
98   if (Section != CurSection) {
99     CurSection = Section;
100     Section->PrintSwitchToSection(MAI, OS);
101   }
102 }
103
104 void MCAsmStreamer::EmitLabel(MCSymbol *Symbol) {
105   assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
106   assert(CurSection && "Cannot emit before setting section!");
107
108   OS << *Symbol << ":\n";
109   Symbol->setSection(*CurSection);
110 }
111
112 void MCAsmStreamer::EmitAssemblerFlag(AssemblerFlag Flag) {
113   switch (Flag) {
114   default: assert(0 && "Invalid flag!");
115   case SubsectionsViaSymbols: OS << ".subsections_via_symbols"; break;
116   }
117   OS << '\n';
118 }
119
120 void MCAsmStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
121   // Only absolute symbols can be redefined.
122   assert((Symbol->isUndefined() || Symbol->isAbsolute()) &&
123          "Cannot define a symbol twice!");
124
125   OS << *Symbol << " = " << *Value << '\n';
126
127   // FIXME: Lift context changes into super class.
128   // FIXME: Set associated section.
129   Symbol->setValue(Value);
130 }
131
132 void MCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
133                                         SymbolAttr Attribute) {
134   switch (Attribute) {
135   case Global:         OS << ".globl";           break;
136   case Hidden:         OS << ".hidden";          break;
137   case IndirectSymbol: OS << ".indirect_symbol"; break;
138   case Internal:       OS << ".internal";        break;
139   case LazyReference:  OS << ".lazy_reference";  break;
140   case NoDeadStrip:    OS << ".no_dead_strip";   break;
141   case PrivateExtern:  OS << ".private_extern";  break;
142   case Protected:      OS << ".protected";       break;
143   case Reference:      OS << ".reference";       break;
144   case Weak:           OS << ".weak";            break;
145   case WeakDefinition: OS << ".weak_definition"; break;
146   case WeakReference:  OS << ".weak_reference";  break;
147   }
148
149   OS << ' ' << *Symbol << '\n';
150 }
151
152 void MCAsmStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
153   OS << ".desc" << ' ' << *Symbol << ',' << DescValue << '\n';
154 }
155
156 void MCAsmStreamer::EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
157                                      unsigned ByteAlignment) {
158   OS << MAI.getCOMMDirective() << *Symbol << ',' << Size;
159   if (ByteAlignment != 0 && MAI.getCOMMDirectiveTakesAlignment()) {
160     if (MAI.getAlignmentIsInBytes())
161       OS << ',' << ByteAlignment;
162     else
163       OS << ',' << Log2_32(ByteAlignment);
164   }
165   OS << '\n';
166 }
167
168 void MCAsmStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
169                                  unsigned Size, unsigned ByteAlignment) {
170   // Note: a .zerofill directive does not switch sections.
171   OS << ".zerofill ";
172   
173   // This is a mach-o specific directive.
174   const MCSectionMachO *MOSection = ((const MCSectionMachO*)Section);
175   OS << MOSection->getSegmentName() << "," << MOSection->getSectionName();
176   
177   if (Symbol != NULL) {
178     OS << ',' << *Symbol << ',' << Size;
179     if (ByteAlignment != 0)
180       OS << ',' << Log2_32(ByteAlignment);
181   }
182   OS << '\n';
183 }
184
185 void MCAsmStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {
186   assert(CurSection && "Cannot emit contents before setting section!");
187   const char *Directive = MAI.getData8bitsDirective(AddrSpace);
188   for (unsigned i = 0, e = Data.size(); i != e; ++i)
189     OS << Directive << (unsigned)(unsigned char)Data[i] << '\n';
190 }
191
192 /// EmitIntValue - Special case of EmitValue that avoids the client having
193 /// to pass in a MCExpr for constant integers.
194 void MCAsmStreamer::EmitIntValue(uint64_t Value, unsigned Size,
195                                  unsigned AddrSpace) {
196   assert(CurSection && "Cannot emit contents before setting section!");
197   // Need target hooks to know how to print this.
198   const char *Directive = 0;
199   switch (Size) {
200   default: break;
201   case 1: Directive = MAI.getData8bitsDirective(AddrSpace); break;
202   case 2: Directive = MAI.getData16bitsDirective(AddrSpace); break;
203   case 4: Directive = MAI.getData32bitsDirective(AddrSpace); break;
204   case 8: Directive = MAI.getData64bitsDirective(AddrSpace); break;
205   }
206   
207   assert(Directive && "Invalid size for machine code value!");
208   OS << Directive << truncateToSize(Value, Size) << '\n';
209 }
210
211 void MCAsmStreamer::EmitValue(const MCExpr *Value, unsigned Size,
212                               unsigned AddrSpace) {
213   assert(CurSection && "Cannot emit contents before setting section!");
214   // Need target hooks to know how to print this.
215   const char *Directive = 0;
216   switch (Size) {
217   default: break;
218   case 1: Directive = MAI.getData8bitsDirective(AddrSpace); break;
219   case 2: Directive = MAI.getData16bitsDirective(AddrSpace); break;
220   case 4: Directive = MAI.getData32bitsDirective(AddrSpace); break;
221   case 8: Directive = MAI.getData64bitsDirective(AddrSpace); break;
222   }
223   
224   assert(Directive && "Invalid size for machine code value!");
225   OS << Directive << *truncateToSize(Value, Size) << '\n';
226 }
227
228 /// EmitFill - Emit NumBytes bytes worth of the value specified by
229 /// FillValue.  This implements directives such as '.space'.
230 void MCAsmStreamer::EmitFill(uint64_t NumBytes, uint8_t FillValue,
231                              unsigned AddrSpace) {
232   if (NumBytes == 0) return;
233   
234   if (AddrSpace == 0)
235     if (const char *ZeroDirective = MAI.getZeroDirective()) {
236       OS << ZeroDirective << NumBytes;
237       if (FillValue != 0)
238         OS << ',' << (int)FillValue;
239       OS << '\n';
240       return;
241     }
242
243   // Emit a byte at a time.
244   MCStreamer::EmitFill(NumBytes, FillValue, AddrSpace);
245 }
246
247 void MCAsmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
248                                          unsigned ValueSize,
249                                          unsigned MaxBytesToEmit) {
250   // Some assemblers don't support non-power of two alignments, so we always
251   // emit alignments as a power of two if possible.
252   if (isPowerOf2_32(ByteAlignment)) {
253     switch (ValueSize) {
254     default: llvm_unreachable("Invalid size for machine code value!");
255     case 1: OS << MAI.getAlignDirective(); break;
256     // FIXME: use MAI for this!
257     case 2: OS << ".p2alignw "; break;
258     case 4: OS << ".p2alignl "; break;
259     case 8: llvm_unreachable("Unsupported alignment size!");
260     }
261     
262     if (MAI.getAlignmentIsInBytes())
263       OS << ByteAlignment;
264     else
265       OS << Log2_32(ByteAlignment);
266
267     if (Value || MaxBytesToEmit) {
268       OS << ", 0x";
269       OS.write_hex(truncateToSize(Value, ValueSize));
270
271       if (MaxBytesToEmit) 
272         OS << ", " << MaxBytesToEmit;
273     }
274     OS << '\n';
275     return;
276   }
277   
278   // Non-power of two alignment.  This is not widely supported by assemblers.
279   // FIXME: Parameterize this based on MAI.
280   switch (ValueSize) {
281   default: llvm_unreachable("Invalid size for machine code value!");
282   case 1: OS << ".balign";  break;
283   case 2: OS << ".balignw"; break;
284   case 4: OS << ".balignl"; break;
285   case 8: llvm_unreachable("Unsupported alignment size!");
286   }
287
288   OS << ' ' << ByteAlignment;
289   OS << ", " << truncateToSize(Value, ValueSize);
290   if (MaxBytesToEmit) 
291     OS << ", " << MaxBytesToEmit;
292   OS << '\n';
293 }
294
295 void MCAsmStreamer::EmitValueToOffset(const MCExpr *Offset,
296                                       unsigned char Value) {
297   // FIXME: Verify that Offset is associated with the current section.
298   OS << ".org " << *Offset << ", " << (unsigned) Value << '\n';
299 }
300
301 void MCAsmStreamer::EmitInstruction(const MCInst &Inst) {
302   assert(CurSection && "Cannot emit contents before setting section!");
303
304   // If we have an AsmPrinter, use that to print.
305   if (InstPrinter) {
306     InstPrinter->printInst(&Inst);
307     OS << '\n';
308
309     // Show the encoding if we have a code emitter.
310     if (Emitter) {
311       SmallString<256> Code;
312       raw_svector_ostream VecOS(Code);
313       Emitter->EncodeInstruction(Inst, VecOS);
314       VecOS.flush();
315   
316       OS.indent(20);
317       OS << " # encoding: [";
318       for (unsigned i = 0, e = Code.size(); i != e; ++i) {
319         if (i)
320           OS << ',';
321         OS << format("%#04x", uint8_t(Code[i]));
322       }
323       OS << "]\n";
324     }
325
326     return;
327   }
328
329   // Otherwise fall back to a structural printing for now. Eventually we should
330   // always have access to the target specific printer.
331   Inst.print(OS, &MAI);
332   OS << '\n';
333 }
334
335 void MCAsmStreamer::Finish() {
336   OS.flush();
337 }
338     
339 MCStreamer *llvm::createAsmStreamer(MCContext &Context, raw_ostream &OS,
340                                     const MCAsmInfo &MAI, MCInstPrinter *IP,
341                                     MCCodeEmitter *CE) {
342   return new MCAsmStreamer(Context, OS, MAI, IP, CE);
343 }