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