]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/MC/MCAsmStreamer.cpp
Update LLVM to r89205.
[FreeBSD/FreeBSD.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/MC/MCAsmInfo.h"
13 #include "llvm/MC/MCCodeEmitter.h"
14 #include "llvm/MC/MCContext.h"
15 #include "llvm/MC/MCExpr.h"
16 #include "llvm/MC/MCInst.h"
17 #include "llvm/MC/MCInstPrinter.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   MCInstPrinter *InstPrinter;
32   MCCodeEmitter *Emitter;
33 public:
34   MCAsmStreamer(MCContext &Context, raw_ostream &_OS, const MCAsmInfo &tai,
35                 MCInstPrinter *_Printer, MCCodeEmitter *_Emitter)
36     : MCStreamer(Context), OS(_OS), MAI(tai), InstPrinter(_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(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   Symbol->print(OS, &MAI);
105   OS << ":\n";
106   Symbol->setSection(*CurSection);
107 }
108
109 void MCAsmStreamer::EmitAssemblerFlag(AssemblerFlag Flag) {
110   switch (Flag) {
111   default: assert(0 && "Invalid flag!");
112   case SubsectionsViaSymbols: OS << ".subsections_via_symbols"; break;
113   }
114   OS << '\n';
115 }
116
117 void MCAsmStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
118   // Only absolute symbols can be redefined.
119   assert((Symbol->isUndefined() || Symbol->isAbsolute()) &&
120          "Cannot define a symbol twice!");
121
122   Symbol->print(OS, &MAI);
123   OS << " = ";
124   Value->print(OS, &MAI);
125   OS << '\n';
126
127   // FIXME: Lift context changes into super class.
128   // FIXME: Set associated section.
129   Symbol->setValue(Value);
130 }
131
132 void MCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
133                                         SymbolAttr Attribute) {
134   switch (Attribute) {
135   case Global:         OS << ".globl";           break;
136   case Hidden:         OS << ".hidden";          break;
137   case IndirectSymbol: OS << ".indirect_symbol"; break;
138   case Internal:       OS << ".internal";        break;
139   case LazyReference:  OS << ".lazy_reference";  break;
140   case NoDeadStrip:    OS << ".no_dead_strip";   break;
141   case PrivateExtern:  OS << ".private_extern";  break;
142   case Protected:      OS << ".protected";       break;
143   case Reference:      OS << ".reference";       break;
144   case Weak:           OS << ".weak";            break;
145   case WeakDefinition: OS << ".weak_definition"; break;
146   case WeakReference:  OS << ".weak_reference";  break;
147   }
148
149   OS << ' ';
150   Symbol->print(OS, &MAI);
151   OS << '\n';
152 }
153
154 void MCAsmStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
155   OS << ".desc" << ' ';
156   Symbol->print(OS, &MAI);
157   OS << ',' << DescValue << '\n';
158 }
159
160 void MCAsmStreamer::EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
161                                      unsigned ByteAlignment) {
162   OS << ".comm ";
163   Symbol->print(OS, &MAI);
164   OS << ',' << Size;
165   if (ByteAlignment != 0)
166     OS << ',' << Log2_32(ByteAlignment);
167   OS << '\n';
168 }
169
170 void MCAsmStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
171                                  unsigned Size, unsigned ByteAlignment) {
172   // Note: a .zerofill directive does not switch sections.
173   OS << ".zerofill ";
174   
175   // This is a mach-o specific directive.
176   const MCSectionMachO *MOSection = ((const MCSectionMachO*)Section);
177   OS << MOSection->getSegmentName() << "," << MOSection->getSectionName();
178   
179   if (Symbol != NULL) {
180     OS << ',';
181     Symbol->print(OS, &MAI);
182     OS << ',' << Size;
183     if (ByteAlignment != 0)
184       OS << ',' << Log2_32(ByteAlignment);
185   }
186   OS << '\n';
187 }
188
189 void MCAsmStreamer::EmitBytes(StringRef Data) {
190   assert(CurSection && "Cannot emit contents before setting section!");
191   for (unsigned i = 0, e = Data.size(); i != e; ++i)
192     OS << ".byte " << (unsigned) (unsigned char) Data[i] << '\n';
193 }
194
195 void MCAsmStreamer::EmitValue(const MCExpr *Value, unsigned Size) {
196   assert(CurSection && "Cannot emit contents before setting section!");
197   // Need target hooks to know how to print this.
198   switch (Size) {
199   default:
200     llvm_unreachable("Invalid size for machine code value!");
201   case 1: OS << ".byte"; break;
202   case 2: OS << ".short"; break;
203   case 4: OS << ".long"; break;
204   case 8: OS << ".quad"; break;
205   }
206
207   OS << ' ';
208   truncateToSize(Value, Size)->print(OS, &MAI);
209   OS << '\n';
210 }
211
212 void MCAsmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
213                                          unsigned ValueSize,
214                                          unsigned MaxBytesToEmit) {
215   // Some assemblers don't support non-power of two alignments, so we always
216   // emit alignments as a power of two if possible.
217   if (isPowerOf2_32(ByteAlignment)) {
218     switch (ValueSize) {
219     default: llvm_unreachable("Invalid size for machine code value!");
220     case 1: OS << MAI.getAlignDirective(); break;
221     // FIXME: use MAI for this!
222     case 2: OS << ".p2alignw "; break;
223     case 4: OS << ".p2alignl "; break;
224     case 8: llvm_unreachable("Unsupported alignment size!");
225     }
226     
227     if (MAI.getAlignmentIsInBytes())
228       OS << ByteAlignment;
229     else
230       OS << Log2_32(ByteAlignment);
231
232     if (Value || MaxBytesToEmit) {
233       OS << ", 0x";
234       OS.write_hex(truncateToSize(Value, ValueSize));
235
236       if (MaxBytesToEmit) 
237         OS << ", " << MaxBytesToEmit;
238     }
239     OS << '\n';
240     return;
241   }
242   
243   // Non-power of two alignment.  This is not widely supported by assemblers.
244   // FIXME: Parameterize this based on MAI.
245   switch (ValueSize) {
246   default: llvm_unreachable("Invalid size for machine code value!");
247   case 1: OS << ".balign";  break;
248   case 2: OS << ".balignw"; break;
249   case 4: OS << ".balignl"; break;
250   case 8: llvm_unreachable("Unsupported alignment size!");
251   }
252
253   OS << ' ' << ByteAlignment;
254   OS << ", " << truncateToSize(Value, ValueSize);
255   if (MaxBytesToEmit) 
256     OS << ", " << MaxBytesToEmit;
257   OS << '\n';
258 }
259
260 void MCAsmStreamer::EmitValueToOffset(const MCExpr *Offset,
261                                       unsigned char Value) {
262   // FIXME: Verify that Offset is associated with the current section.
263   OS << ".org ";
264   Offset->print(OS, &MAI);
265   OS << ", " << (unsigned) Value << '\n';
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 (InstPrinter) {
273     InstPrinter->printInst(&Inst);
274     OS << '\n';
275
276     // Show the encoding if we have a code emitter.
277     if (Emitter) {
278       SmallString<256> Code;
279       raw_svector_ostream VecOS(Code);
280       Emitter->EncodeInstruction(Inst, VecOS);
281       VecOS.flush();
282   
283       OS.indent(20);
284       OS << " # encoding: [";
285       for (unsigned i = 0, e = Code.size(); i != e; ++i) {
286         if (i)
287           OS << ',';
288         OS << format("%#04x", uint8_t(Code[i]));
289       }
290       OS << "]\n";
291     }
292
293     return;
294   }
295
296   // Otherwise fall back to a structural printing for now. Eventually we should
297   // always have access to the target specific printer.
298   Inst.print(OS, &MAI);
299   OS << '\n';
300 }
301
302 void MCAsmStreamer::Finish() {
303   OS.flush();
304 }
305     
306 MCStreamer *llvm::createAsmStreamer(MCContext &Context, raw_ostream &OS,
307                                     const MCAsmInfo &MAI, MCInstPrinter *IP,
308                                     MCCodeEmitter *CE) {
309   return new MCAsmStreamer(Context, OS, MAI, IP, CE);
310 }