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