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