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