inline insertion operators.
[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/CodeGen/AsmPrinter.h"
13 #include "llvm/MC/MCAsmInfo.h"
14 #include "llvm/MC/MCCodeEmitter.h"
15 #include "llvm/MC/MCContext.h"
16 #include "llvm/MC/MCExpr.h"
17 #include "llvm/MC/MCInst.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   AsmPrinter *Printer;
32   MCCodeEmitter *Emitter;
33 public:
34   MCAsmStreamer(MCContext &Context, raw_ostream &_OS, const MCAsmInfo &tai,
35                 AsmPrinter *_Printer, MCCodeEmitter *_Emitter)
36     : MCStreamer(Context), OS(_OS), MAI(tai), Printer(_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(const StringRef &Data);
62
63   virtual void EmitValue(const MCExpr *Value, unsigned Size);
64
65   virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
66                                     unsigned ValueSize = 1,
67                                     unsigned MaxBytesToEmit = 0);
68
69   virtual void EmitValueToOffset(const MCExpr *Offset,
70                                  unsigned char Value = 0);
71   
72   virtual void EmitInstruction(const MCInst &Inst);
73
74   virtual void Finish();
75   
76   /// @}
77 };
78
79 } // end anonymous namespace.
80
81 static inline int64_t truncateToSize(int64_t Value, unsigned Bytes) {
82   assert(Bytes && "Invalid size!");
83   return Value & ((uint64_t) (int64_t) -1 >> (64 - Bytes * 8));
84 }
85
86 static inline const MCExpr *truncateToSize(const MCExpr *Value,
87                                            unsigned Bytes) {
88   // FIXME: Do we really need this routine?
89   return Value;
90 }
91
92 void MCAsmStreamer::SwitchSection(const MCSection *Section) {
93   assert(Section && "Cannot switch to a null section!");
94   if (Section != CurSection) {
95     CurSection = Section;
96     Section->PrintSwitchToSection(MAI, OS);
97   }
98 }
99
100 void MCAsmStreamer::EmitLabel(MCSymbol *Symbol) {
101   assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
102   assert(CurSection && "Cannot emit before setting section!");
103
104   OS << Symbol << ":\n";
105   Symbol->setSection(*CurSection);
106 }
107
108 void MCAsmStreamer::EmitAssemblerFlag(AssemblerFlag Flag) {
109   switch (Flag) {
110   default: assert(0 && "Invalid flag!");
111   case SubsectionsViaSymbols: OS << ".subsections_via_symbols"; break;
112   }
113   OS << '\n';
114 }
115
116 void MCAsmStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
117   // Only absolute symbols can be redefined.
118   assert((Symbol->isUndefined() || Symbol->isAbsolute()) &&
119          "Cannot define a symbol twice!");
120
121   OS << Symbol << " = ";
122   Value->print(OS);
123   OS << '\n';
124 }
125
126 void MCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol, 
127                                         SymbolAttr Attribute) {
128   switch (Attribute) {
129   case Global: OS << ".globl"; break;
130   case Hidden: OS << ".hidden"; break;
131   case IndirectSymbol: OS << ".indirect_symbol"; break;
132   case Internal: OS << ".internal"; break;
133   case LazyReference: OS << ".lazy_reference"; break;
134   case NoDeadStrip: OS << ".no_dead_strip"; break;
135   case PrivateExtern: OS << ".private_extern"; break;
136   case Protected: OS << ".protected"; break;
137   case Reference: OS << ".reference"; break;
138   case Weak: OS << ".weak"; break;
139   case WeakDefinition: OS << ".weak_definition"; break;
140   case WeakReference: OS << ".weak_reference"; break;
141   }
142
143   OS << ' ' << Symbol << '\n';
144 }
145
146 void MCAsmStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
147   OS << ".desc" << ' ' << Symbol << ',' << DescValue << '\n';
148 }
149
150 void MCAsmStreamer::EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
151                                      unsigned ByteAlignment) {
152   OS << ".comm";
153   OS << ' ' << Symbol << ',' << Size;
154   if (ByteAlignment != 0)
155     OS << ',' << Log2_32(ByteAlignment);
156   OS << '\n';
157 }
158
159 void MCAsmStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
160                                  unsigned Size, unsigned ByteAlignment) {
161   // Note: a .zerofill directive does not switch sections.
162   OS << ".zerofill ";
163   
164   // This is a mach-o specific directive.
165   const MCSectionMachO *MOSection = ((const MCSectionMachO*)Section);
166   OS << MOSection->getSegmentName() << "," << MOSection->getSectionName();
167   
168   if (Symbol != NULL) {
169     OS << ',' << Symbol << ',' << Size;
170     if (ByteAlignment != 0)
171       OS << ',' << Log2_32(ByteAlignment);
172   }
173   OS << '\n';
174 }
175
176 void MCAsmStreamer::EmitBytes(const StringRef &Data) {
177   assert(CurSection && "Cannot emit contents before setting section!");
178   for (unsigned i = 0, e = Data.size(); i != e; ++i)
179     OS << ".byte " << (unsigned) (unsigned char) Data[i] << '\n';
180 }
181
182 void MCAsmStreamer::EmitValue(const MCExpr *Value, unsigned Size) {
183   assert(CurSection && "Cannot emit contents before setting section!");
184   // Need target hooks to know how to print this.
185   switch (Size) {
186   default:
187     llvm_unreachable("Invalid size for machine code value!");
188   case 1: OS << ".byte"; break;
189   case 2: OS << ".short"; break;
190   case 4: OS << ".long"; break;
191   case 8: OS << ".quad"; break;
192   }
193
194   OS << ' ';
195   truncateToSize(Value, Size)->print(OS);
196   OS << '\n';
197 }
198
199 void MCAsmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
200                                          unsigned ValueSize,
201                                          unsigned MaxBytesToEmit) {
202   // Some assemblers don't support non-power of two alignments, so we always
203   // emit alignments as a power of two if possible.
204   if (isPowerOf2_32(ByteAlignment)) {
205     switch (ValueSize) {
206     default: llvm_unreachable("Invalid size for machine code value!");
207     case 1: OS << MAI.getAlignDirective(); break;
208     // FIXME: use MAI for this!
209     case 2: OS << ".p2alignw "; break;
210     case 4: OS << ".p2alignl "; break;
211     case 8: llvm_unreachable("Unsupported alignment size!");
212     }
213     
214     if (MAI.getAlignmentIsInBytes())
215       OS << ByteAlignment;
216     else
217       OS << Log2_32(ByteAlignment);
218
219     if (Value || MaxBytesToEmit) {
220       OS << ", 0x";
221       OS.write_hex(truncateToSize(Value, ValueSize));
222
223       if (MaxBytesToEmit) 
224         OS << ", " << MaxBytesToEmit;
225     }
226     OS << '\n';
227     return;
228   }
229   
230   // Non-power of two alignment.  This is not widely supported by assemblers.
231   // FIXME: Parameterize this based on MAI.
232   switch (ValueSize) {
233   default: llvm_unreachable("Invalid size for machine code value!");
234   case 1: OS << ".balign";  break;
235   case 2: OS << ".balignw"; break;
236   case 4: OS << ".balignl"; break;
237   case 8: llvm_unreachable("Unsupported alignment size!");
238   }
239
240   OS << ' ' << ByteAlignment;
241   OS << ", " << truncateToSize(Value, ValueSize);
242   if (MaxBytesToEmit) 
243     OS << ", " << MaxBytesToEmit;
244   OS << '\n';
245 }
246
247 void MCAsmStreamer::EmitValueToOffset(const MCExpr *Offset,
248                                       unsigned char Value) {
249   // FIXME: Verify that Offset is associated with the current section.
250   OS << ".org ";
251   Offset->print(OS);
252   OS << ", " << (unsigned) Value << '\n';
253 }
254
255 void MCAsmStreamer::EmitInstruction(const MCInst &Inst) {
256   assert(CurSection && "Cannot emit contents before setting section!");
257
258   // If we have an AsmPrinter, use that to print.
259   if (Printer) {
260     Printer->printMCInst(&Inst);
261
262     // Show the encoding if we have a code emitter.
263     if (Emitter) {
264       SmallString<256> Code;
265       raw_svector_ostream VecOS(Code);
266       Emitter->EncodeInstruction(Inst, VecOS);
267       VecOS.flush();
268   
269       OS.indent(20);
270       OS << " # encoding: [";
271       for (unsigned i = 0, e = Code.size(); i != e; ++i) {
272         if (i)
273           OS << ',';
274         OS << format("%#04x", uint8_t(Code[i]));
275       }
276       OS << "]\n";
277     }
278
279     return;
280   }
281
282   // Otherwise fall back to a structural printing for now. Eventually we should
283   // always have access to the target specific printer.
284   Inst.print(OS);
285   OS << '\n';
286 }
287
288 void MCAsmStreamer::Finish() {
289   OS.flush();
290 }
291     
292 MCStreamer *llvm::createAsmStreamer(MCContext &Context, raw_ostream &OS,
293                                     const MCAsmInfo &MAI, AsmPrinter *AP,
294                                     MCCodeEmitter *CE) {
295   return new MCAsmStreamer(Context, OS, MAI, AP, CE);
296 }