Move EmitValue to MCObjectStreamer.
[oota-llvm.git] / lib / MC / MCMachOStreamer.cpp
1 //===- lib/MC/MCMachOStreamer.cpp - Mach-O Object 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
12 #include "llvm/MC/MCAssembler.h"
13 #include "llvm/MC/MCContext.h"
14 #include "llvm/MC/MCCodeEmitter.h"
15 #include "llvm/MC/MCExpr.h"
16 #include "llvm/MC/MCInst.h"
17 #include "llvm/MC/MCObjectStreamer.h"
18 #include "llvm/MC/MCSection.h"
19 #include "llvm/MC/MCSymbol.h"
20 #include "llvm/MC/MCMachOSymbolFlags.h"
21 #include "llvm/MC/MCSectionMachO.h"
22 #include "llvm/MC/MCDwarf.h"
23 #include "llvm/Support/Dwarf.h"
24 #include "llvm/Support/ErrorHandling.h"
25 #include "llvm/Support/raw_ostream.h"
26 #include "llvm/Target/TargetAsmBackend.h"
27
28 using namespace llvm;
29
30 namespace {
31
32 class MCMachOStreamer : public MCObjectStreamer {
33 private:
34   virtual void EmitInstToFragment(const MCInst &Inst);
35   virtual void EmitInstToData(const MCInst &Inst);
36
37 public:
38   MCMachOStreamer(MCContext &Context, TargetAsmBackend &TAB,
39                   raw_ostream &OS, MCCodeEmitter *Emitter)
40     : MCObjectStreamer(Context, TAB, OS, Emitter, true) {}
41
42   /// @name MCStreamer Interface
43   /// @{
44
45   virtual void InitSections();
46   virtual void EmitLabel(MCSymbol *Symbol);
47   virtual void EmitAssemblerFlag(MCAssemblerFlag Flag);
48   virtual void EmitThumbFunc(MCSymbol *Func);
49   virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value);
50   virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute);
51   virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
52   virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
53                                 unsigned ByteAlignment);
54   virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol) {
55     assert(0 && "macho doesn't support this directive");
56   }
57   virtual void EmitCOFFSymbolStorageClass(int StorageClass) {
58     assert(0 && "macho doesn't support this directive");
59   }
60   virtual void EmitCOFFSymbolType(int Type) {
61     assert(0 && "macho doesn't support this directive");
62   }
63   virtual void EndCOFFSymbolDef() {
64     assert(0 && "macho doesn't support this directive");
65   }
66   virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
67     assert(0 && "macho doesn't support this directive");
68   }
69   virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size) {
70     assert(0 && "macho doesn't support this directive");
71   }
72   virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
73                             unsigned Size = 0, unsigned ByteAlignment = 0);
74   virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
75                               uint64_t Size, unsigned ByteAlignment = 0);
76   virtual void EmitBytes(StringRef Data, unsigned AddrSpace);
77   virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
78                                     unsigned ValueSize = 1,
79                                     unsigned MaxBytesToEmit = 0);
80   virtual void EmitCodeAlignment(unsigned ByteAlignment,
81                                  unsigned MaxBytesToEmit = 0);
82   virtual void EmitValueToOffset(const MCExpr *Offset,
83                                  unsigned char Value = 0);
84
85   virtual void EmitFileDirective(StringRef Filename) {
86     // FIXME: Just ignore the .file; it isn't important enough to fail the
87     // entire assembly.
88
89     //report_fatal_error("unsupported directive: '.file'");
90   }
91
92   virtual void Finish();
93
94   /// @}
95 };
96
97 } // end anonymous namespace.
98
99 void MCMachOStreamer::InitSections() {
100   SwitchSection(getContext().getMachOSection("__TEXT", "__text",
101                                     MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
102                                     0, SectionKind::getText()));
103
104 }
105
106 void MCMachOStreamer::EmitLabel(MCSymbol *Symbol) {
107   assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
108
109   // isSymbolLinkerVisible uses the section.
110   Symbol->setSection(*CurSection);
111   // We have to create a new fragment if this is an atom defining symbol,
112   // fragments cannot span atoms.
113   if (getAssembler().isSymbolLinkerVisible(*Symbol))
114     new MCDataFragment(getCurrentSectionData());
115
116   MCObjectStreamer::EmitLabel(Symbol);
117
118   MCSymbolData &SD = getAssembler().getSymbolData(*Symbol);
119   // This causes the reference type flag to be cleared. Darwin 'as' was "trying"
120   // to clear the weak reference and weak definition bits too, but the
121   // implementation was buggy. For now we just try to match 'as', for
122   // diffability.
123   //
124   // FIXME: Cleanup this code, these bits should be emitted based on semantic
125   // properties, not on the order of definition, etc.
126   SD.setFlags(SD.getFlags() & ~SF_ReferenceTypeMask);
127 }
128
129 void MCMachOStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
130   switch (Flag) {
131   case MCAF_SyntaxUnified: return; // no-op here.
132   case MCAF_Code16: return; // no-op here.
133   case MCAF_Code32: return; // no-op here.
134   case MCAF_SubsectionsViaSymbols:
135     getAssembler().setSubsectionsViaSymbols(true);
136     return;
137   default:
138     llvm_unreachable("invalid assembler flag!");
139   }
140 }
141
142 void MCMachOStreamer::EmitThumbFunc(MCSymbol *Func) {
143   // FIXME: Flag the function ISA as thumb with DW_AT_APPLE_isa.
144 }
145
146 void MCMachOStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
147   // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
148   // MCObjectStreamer.
149   // FIXME: Lift context changes into super class.
150   getAssembler().getOrCreateSymbolData(*Symbol);
151   Symbol->setVariableValue(AddValueSymbols(Value));
152 }
153
154 void MCMachOStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
155                                           MCSymbolAttr Attribute) {
156   // Indirect symbols are handled differently, to match how 'as' handles
157   // them. This makes writing matching .o files easier.
158   if (Attribute == MCSA_IndirectSymbol) {
159     // Note that we intentionally cannot use the symbol data here; this is
160     // important for matching the string table that 'as' generates.
161     IndirectSymbolData ISD;
162     ISD.Symbol = Symbol;
163     ISD.SectionData = getCurrentSectionData();
164     getAssembler().getIndirectSymbols().push_back(ISD);
165     return;
166   }
167
168   // Adding a symbol attribute always introduces the symbol, note that an
169   // important side effect of calling getOrCreateSymbolData here is to register
170   // the symbol with the assembler.
171   MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
172
173   // The implementation of symbol attributes is designed to match 'as', but it
174   // leaves much to desired. It doesn't really make sense to arbitrarily add and
175   // remove flags, but 'as' allows this (in particular, see .desc).
176   //
177   // In the future it might be worth trying to make these operations more well
178   // defined.
179   switch (Attribute) {
180   case MCSA_Invalid:
181   case MCSA_ELF_TypeFunction:
182   case MCSA_ELF_TypeIndFunction:
183   case MCSA_ELF_TypeObject:
184   case MCSA_ELF_TypeTLS:
185   case MCSA_ELF_TypeCommon:
186   case MCSA_ELF_TypeNoType:
187   case MCSA_ELF_TypeGnuUniqueObject:
188   case MCSA_IndirectSymbol:
189   case MCSA_Hidden:
190   case MCSA_Internal:
191   case MCSA_Protected:
192   case MCSA_Weak:
193   case MCSA_Local:
194     assert(0 && "Invalid symbol attribute for Mach-O!");
195     break;
196
197   case MCSA_Global:
198     SD.setExternal(true);
199     // This effectively clears the undefined lazy bit, in Darwin 'as', although
200     // it isn't very consistent because it implements this as part of symbol
201     // lookup.
202     //
203     // FIXME: Cleanup this code, these bits should be emitted based on semantic
204     // properties, not on the order of definition, etc.
205     SD.setFlags(SD.getFlags() & ~SF_ReferenceTypeUndefinedLazy);
206     break;
207
208   case MCSA_LazyReference:
209     // FIXME: This requires -dynamic.
210     SD.setFlags(SD.getFlags() | SF_NoDeadStrip);
211     if (Symbol->isUndefined())
212       SD.setFlags(SD.getFlags() | SF_ReferenceTypeUndefinedLazy);
213     break;
214
215     // Since .reference sets the no dead strip bit, it is equivalent to
216     // .no_dead_strip in practice.
217   case MCSA_Reference:
218   case MCSA_NoDeadStrip:
219     SD.setFlags(SD.getFlags() | SF_NoDeadStrip);
220     break;
221
222   case MCSA_SymbolResolver:
223     SD.setFlags(SD.getFlags() | SF_SymbolResolver);
224     break;
225
226   case MCSA_PrivateExtern:
227     SD.setExternal(true);
228     SD.setPrivateExtern(true);
229     break;
230
231   case MCSA_WeakReference:
232     // FIXME: This requires -dynamic.
233     if (Symbol->isUndefined())
234       SD.setFlags(SD.getFlags() | SF_WeakReference);
235     break;
236
237   case MCSA_WeakDefinition:
238     // FIXME: 'as' enforces that this is defined and global. The manual claims
239     // it has to be in a coalesced section, but this isn't enforced.
240     SD.setFlags(SD.getFlags() | SF_WeakDefinition);
241     break;
242
243   case MCSA_WeakDefAutoPrivate:
244     SD.setFlags(SD.getFlags() | SF_WeakDefinition | SF_WeakReference);
245     break;
246   }
247 }
248
249 void MCMachOStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
250   // Encode the 'desc' value into the lowest implementation defined bits.
251   assert(DescValue == (DescValue & SF_DescFlagsMask) &&
252          "Invalid .desc value!");
253   getAssembler().getOrCreateSymbolData(*Symbol).setFlags(
254     DescValue & SF_DescFlagsMask);
255 }
256
257 void MCMachOStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
258                                        unsigned ByteAlignment) {
259   // FIXME: Darwin 'as' does appear to allow redef of a .comm by itself.
260   assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
261
262   MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
263   SD.setExternal(true);
264   SD.setCommon(Size, ByteAlignment);
265 }
266
267 void MCMachOStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
268                                    unsigned Size, unsigned ByteAlignment) {
269   MCSectionData &SectData = getAssembler().getOrCreateSectionData(*Section);
270
271   // The symbol may not be present, which only creates the section.
272   if (!Symbol)
273     return;
274
275   // FIXME: Assert that this section has the zerofill type.
276
277   assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
278
279   MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
280
281   // Emit an align fragment if necessary.
282   if (ByteAlignment != 1)
283     new MCAlignFragment(ByteAlignment, 0, 0, ByteAlignment, &SectData);
284
285   MCFragment *F = new MCFillFragment(0, 0, Size, &SectData);
286   SD.setFragment(F);
287
288   Symbol->setSection(*Section);
289
290   // Update the maximum alignment on the zero fill section if necessary.
291   if (ByteAlignment > SectData.getAlignment())
292     SectData.setAlignment(ByteAlignment);
293 }
294
295 // This should always be called with the thread local bss section.  Like the
296 // .zerofill directive this doesn't actually switch sections on us.
297 void MCMachOStreamer::EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
298                                      uint64_t Size, unsigned ByteAlignment) {
299   EmitZerofill(Section, Symbol, Size, ByteAlignment);
300   return;
301 }
302
303 void MCMachOStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {
304   // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
305   // MCObjectStreamer.
306   getOrCreateDataFragment()->getContents().append(Data.begin(), Data.end());
307 }
308
309 void MCMachOStreamer::EmitValueToAlignment(unsigned ByteAlignment,
310                                            int64_t Value, unsigned ValueSize,
311                                            unsigned MaxBytesToEmit) {
312   // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
313   // MCObjectStreamer.
314   if (MaxBytesToEmit == 0)
315     MaxBytesToEmit = ByteAlignment;
316   new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit,
317                       getCurrentSectionData());
318
319   // Update the maximum alignment on the current section if necessary.
320   if (ByteAlignment > getCurrentSectionData()->getAlignment())
321     getCurrentSectionData()->setAlignment(ByteAlignment);
322 }
323
324 void MCMachOStreamer::EmitCodeAlignment(unsigned ByteAlignment,
325                                         unsigned MaxBytesToEmit) {
326   // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
327   // MCObjectStreamer.
328   if (MaxBytesToEmit == 0)
329     MaxBytesToEmit = ByteAlignment;
330   MCAlignFragment *F = new MCAlignFragment(ByteAlignment, 0, 1, MaxBytesToEmit,
331                                            getCurrentSectionData());
332   F->setEmitNops(true);
333
334   // Update the maximum alignment on the current section if necessary.
335   if (ByteAlignment > getCurrentSectionData()->getAlignment())
336     getCurrentSectionData()->setAlignment(ByteAlignment);
337 }
338
339 void MCMachOStreamer::EmitValueToOffset(const MCExpr *Offset,
340                                         unsigned char Value) {
341   new MCOrgFragment(*Offset, Value, getCurrentSectionData());
342 }
343
344 void MCMachOStreamer::EmitInstToFragment(const MCInst &Inst) {
345   MCInstFragment *IF = new MCInstFragment(Inst, getCurrentSectionData());
346
347   // Add the fixups and data.
348   //
349   // FIXME: Revisit this design decision when relaxation is done, we may be
350   // able to get away with not storing any extra data in the MCInst.
351   SmallVector<MCFixup, 4> Fixups;
352   SmallString<256> Code;
353   raw_svector_ostream VecOS(Code);
354   getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups);
355   VecOS.flush();
356
357   IF->getCode() = Code;
358   IF->getFixups() = Fixups;
359 }
360
361 void MCMachOStreamer::EmitInstToData(const MCInst &Inst) {
362   MCDataFragment *DF = getOrCreateDataFragment();
363
364   SmallVector<MCFixup, 4> Fixups;
365   SmallString<256> Code;
366   raw_svector_ostream VecOS(Code);
367   getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups);
368   VecOS.flush();
369
370   // Add the fixups and data.
371   for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
372     Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size());
373     DF->addFixup(Fixups[i]);
374   }
375   DF->getContents().append(Code.begin(), Code.end());
376 }
377
378 void MCMachOStreamer::Finish() {
379   // Dump out the dwarf file & directory tables and line tables.
380   if (getContext().hasDwarfFiles()) {
381     const MCSection *DwarfLineSection = getContext().getMachOSection("__DWARF",
382                                          "__debug_line",
383                                          MCSectionMachO::S_ATTR_DEBUG,
384                                          0, SectionKind::getDataRelLocal());
385     MCSectionData &DLS =
386       getAssembler().getOrCreateSectionData(*DwarfLineSection);
387     int PointerSize = getAssembler().getBackend().getPointerSize();
388     MCDwarfFileTable::Emit(this, DwarfLineSection, &DLS, PointerSize);
389   }
390
391   // We have to set the fragment atom associations so we can relax properly for
392   // Mach-O.
393
394   // First, scan the symbol table to build a lookup table from fragments to
395   // defining symbols.
396   DenseMap<const MCFragment*, MCSymbolData*> DefiningSymbolMap;
397   for (MCAssembler::symbol_iterator it = getAssembler().symbol_begin(),
398          ie = getAssembler().symbol_end(); it != ie; ++it) {
399     if (getAssembler().isSymbolLinkerVisible(it->getSymbol()) &&
400         it->getFragment()) {
401       // An atom defining symbol should never be internal to a fragment.
402       assert(it->getOffset() == 0 && "Invalid offset in atom defining symbol!");
403       DefiningSymbolMap[it->getFragment()] = it;
404     }
405   }
406
407   // Set the fragment atom associations by tracking the last seen atom defining
408   // symbol.
409   for (MCAssembler::iterator it = getAssembler().begin(),
410          ie = getAssembler().end(); it != ie; ++it) {
411     MCSymbolData *CurrentAtom = 0;
412     for (MCSectionData::iterator it2 = it->begin(),
413            ie2 = it->end(); it2 != ie2; ++it2) {
414       if (MCSymbolData *SD = DefiningSymbolMap.lookup(it2))
415         CurrentAtom = SD;
416       it2->setAtom(CurrentAtom);
417     }
418   }
419
420   this->MCObjectStreamer::Finish();
421 }
422
423 MCStreamer *llvm::createMachOStreamer(MCContext &Context, TargetAsmBackend &TAB,
424                                       raw_ostream &OS, MCCodeEmitter *CE,
425                                       bool RelaxAll) {
426   MCMachOStreamer *S = new MCMachOStreamer(Context, TAB, OS, CE);
427   if (RelaxAll)
428     S->getAssembler().setRelaxAll(true);
429   return S;
430 }