blob: ffae64209950647e6da86d1e73e8f6aed2cce152 [file] [log] [blame]
Daniel Dunbar3016db32009-08-21 09:11:24 +00001//===- 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
Daniel Dunbar3016db32009-08-21 09:11:24 +000012#include "llvm/MC/MCAssembler.h"
13#include "llvm/MC/MCContext.h"
Daniel Dunbarc7c5f9f2009-08-27 08:17:51 +000014#include "llvm/MC/MCCodeEmitter.h"
Daniel Dunbar73da11e2009-08-31 08:08:38 +000015#include "llvm/MC/MCExpr.h"
Daniel Dunbarc7c5f9f2009-08-27 08:17:51 +000016#include "llvm/MC/MCInst.h"
Daniel Dunbar8a3c9d92010-06-16 20:04:22 +000017#include "llvm/MC/MCObjectStreamer.h"
Daniel Dunbar3016db32009-08-21 09:11:24 +000018#include "llvm/MC/MCSection.h"
19#include "llvm/MC/MCSymbol.h"
Kevin Enderby51bed9c2010-05-07 21:44:23 +000020#include "llvm/MC/MCMachOSymbolFlags.h"
Daniel Dunbar3016db32009-08-21 09:11:24 +000021#include "llvm/Support/ErrorHandling.h"
Daniel Dunbarc7c5f9f2009-08-27 08:17:51 +000022#include "llvm/Support/raw_ostream.h"
Daniel Dunbarde04b3f2010-03-23 05:09:03 +000023#include "llvm/Target/TargetAsmBackend.h"
24
Daniel Dunbar3016db32009-08-21 09:11:24 +000025using namespace llvm;
26
27namespace {
28
Daniel Dunbar8a3c9d92010-06-16 20:04:22 +000029class MCMachOStreamer : public MCObjectStreamer {
Daniel Dunbar04a11582009-08-24 08:40:12 +000030private:
Daniel Dunbar9d40ef12010-05-26 20:37:00 +000031 void EmitInstToFragment(const MCInst &Inst);
32 void EmitInstToData(const MCInst &Inst);
33
Daniel Dunbar3016db32009-08-21 09:11:24 +000034public:
Daniel Dunbarc5ddbad2010-03-11 01:34:27 +000035 MCMachOStreamer(MCContext &Context, TargetAsmBackend &TAB,
Daniel Dunbar8a3c9d92010-06-16 20:04:22 +000036 raw_ostream &OS, MCCodeEmitter *Emitter)
Daniel Dunbarb2347fe2010-06-16 20:04:25 +000037 : MCObjectStreamer(Context, TAB, OS, Emitter) {}
Daniel Dunbard821f4a2010-03-25 22:49:09 +000038
Daniel Dunbar3016db32009-08-21 09:11:24 +000039 /// @name MCStreamer Interface
40 /// @{
41
Daniel Dunbar3016db32009-08-21 09:11:24 +000042 virtual void EmitLabel(MCSymbol *Symbol);
Chris Lattner685508c2010-01-23 06:39:22 +000043 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag);
Daniel Dunbar897ffad2009-08-31 08:09:28 +000044 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value);
Chris Lattner685508c2010-01-23 06:39:22 +000045 virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute);
Daniel Dunbar3016db32009-08-21 09:11:24 +000046 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
Chris Lattnerb1301f72010-01-23 07:47:02 +000047 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Daniel Dunbar6a715dc2009-08-30 06:17:16 +000048 unsigned ByteAlignment);
Chris Lattner72afa952010-05-08 19:54:22 +000049 virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol) {
50 assert(0 && "macho doesn't support this directive");
51 }
52 virtual void EmitCOFFSymbolStorageClass(int StorageClass) {
53 assert(0 && "macho doesn't support this directive");
54 }
55 virtual void EmitCOFFSymbolType(int Type) {
56 assert(0 && "macho doesn't support this directive");
57 }
58 virtual void EndCOFFSymbolDef() {
59 assert(0 && "macho doesn't support this directive");
60 }
Chris Lattner91dac6d2010-01-25 07:52:13 +000061 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
62 assert(0 && "macho doesn't support this directive");
63 }
Chris Lattnerb1301f72010-01-23 07:47:02 +000064 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size) {
65 assert(0 && "macho doesn't support this directive");
66 }
Daniel Dunbarcf72e1c2009-08-28 05:48:22 +000067 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
Daniel Dunbar6a715dc2009-08-30 06:17:16 +000068 unsigned Size = 0, unsigned ByteAlignment = 0);
Eric Christopherfeedc902010-05-18 21:26:41 +000069 virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
70 uint64_t Size, unsigned ByteAlignment = 0);
Chris Lattnerc35681b2010-01-19 19:46:13 +000071 virtual void EmitBytes(StringRef Data, unsigned AddrSpace);
Chris Lattnerc35681b2010-01-19 19:46:13 +000072 virtual void EmitValue(const MCExpr *Value, unsigned Size,unsigned AddrSpace);
Chris Lattner3cde7602010-01-25 21:28:50 +000073 virtual void EmitGPRel32Value(const MCExpr *Value) {
74 assert(0 && "macho doesn't support this directive");
75 }
Daniel Dunbar3016db32009-08-21 09:11:24 +000076 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
77 unsigned ValueSize = 1,
78 unsigned MaxBytesToEmit = 0);
Kevin Enderbye83d74f2010-02-23 18:26:34 +000079 virtual void EmitCodeAlignment(unsigned ByteAlignment,
80 unsigned MaxBytesToEmit = 0);
Daniel Dunbar897ffad2009-08-31 08:09:28 +000081 virtual void EmitValueToOffset(const MCExpr *Offset,
Daniel Dunbar3016db32009-08-21 09:11:24 +000082 unsigned char Value = 0);
Daniel Dunbar9d40ef12010-05-26 20:37:00 +000083
Chris Lattner601ef332010-01-25 18:58:59 +000084 virtual void EmitFileDirective(StringRef Filename) {
Daniel Dunbar39617bb2010-05-18 17:28:17 +000085 report_fatal_error("unsupported directive: '.file'");
Chris Lattner601ef332010-01-25 18:58:59 +000086 }
87 virtual void EmitDwarfFileDirective(unsigned FileNo, StringRef Filename) {
Daniel Dunbar39617bb2010-05-18 17:28:17 +000088 report_fatal_error("unsupported directive: '.file'");
Chris Lattner601ef332010-01-25 18:58:59 +000089 }
Daniel Dunbar9d40ef12010-05-26 20:37:00 +000090
Daniel Dunbar3016db32009-08-21 09:11:24 +000091 virtual void EmitInstruction(const MCInst &Inst);
Daniel Dunbar3016db32009-08-21 09:11:24 +000092
Daniel Dunbarede8e6d2010-06-16 20:04:32 +000093 virtual void Finish();
94
Daniel Dunbar3016db32009-08-21 09:11:24 +000095 /// @}
96};
97
98} // end anonymous namespace.
99
Daniel Dunbar3016db32009-08-21 09:11:24 +0000100void MCMachOStreamer::EmitLabel(MCSymbol *Symbol) {
Michael J. Spencere2da0a42010-07-19 06:13:10 +0000101 // TODO: This is almost exactly the same as WinCOFFStreamer. Consider merging
102 // into MCObjectStreamer.
Daniel Dunbar6860ac72009-08-22 07:22:36 +0000103 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
Daniel Dunbardeb7ba92010-05-05 19:01:00 +0000104 assert(!Symbol->isVariable() && "Cannot emit a variable symbol!");
105 assert(CurSection && "Cannot emit before setting section!");
Daniel Dunbar3016db32009-08-21 09:11:24 +0000106
Daniel Dunbarede8e6d2010-06-16 20:04:32 +0000107 Symbol->setSection(*CurSection);
108
Daniel Dunbar8a3c9d92010-06-16 20:04:22 +0000109 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
Daniel Dunbaraadb2ca2010-05-10 22:45:09 +0000110
Daniel Dunbarede8e6d2010-06-16 20:04:32 +0000111 // We have to create a new fragment if this is an atom defining symbol,
112 // fragments cannot span atoms.
113 if (getAssembler().isSymbolLinkerVisible(SD.getSymbol()))
114 new MCDataFragment(getCurrentSectionData());
Daniel Dunbaraadb2ca2010-05-10 22:45:09 +0000115
Daniel Dunbarf1f18272010-03-22 20:35:46 +0000116 // FIXME: This is wasteful, we don't necessarily need to create a data
117 // fragment. Instead, we should mark the symbol as pointing into the data
118 // fragment if it exists, otherwise we should just queue the label and set its
119 // fragment pointer when we emit the next fragment.
Daniel Dunbarede8e6d2010-06-16 20:04:32 +0000120 MCDataFragment *F = getOrCreateDataFragment();
Daniel Dunbarff3eafd2009-08-22 10:13:24 +0000121 assert(!SD.getFragment() && "Unexpected fragment on symbol data!");
122 SD.setFragment(F);
123 SD.setOffset(F->getContents().size());
Daniel Dunbar04a11582009-08-24 08:40:12 +0000124
Daniel Dunbar0211a962010-05-17 20:12:31 +0000125 // This causes the reference type flag to be cleared. Darwin 'as' was "trying"
126 // to clear the weak reference and weak definition bits too, but the
127 // implementation was buggy. For now we just try to match 'as', for
128 // diffability.
129 //
130 // FIXME: Cleanup this code, these bits should be emitted based on semantic
131 // properties, not on the order of definition, etc.
132 SD.setFlags(SD.getFlags() & ~SF_ReferenceTypeMask);
Daniel Dunbar3016db32009-08-21 09:11:24 +0000133}
134
Chris Lattner685508c2010-01-23 06:39:22 +0000135void MCMachOStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
Daniel Dunbare2697732009-08-26 21:22:22 +0000136 switch (Flag) {
Chris Lattner685508c2010-01-23 06:39:22 +0000137 case MCAF_SubsectionsViaSymbols:
Daniel Dunbar8a3c9d92010-06-16 20:04:22 +0000138 getAssembler().setSubsectionsViaSymbols(true);
Daniel Dunbar2a2459a2009-08-28 07:08:47 +0000139 return;
Daniel Dunbare2697732009-08-26 21:22:22 +0000140 }
Daniel Dunbar2a2459a2009-08-28 07:08:47 +0000141
142 assert(0 && "invalid assembler flag!");
Daniel Dunbar3016db32009-08-21 09:11:24 +0000143}
144
Daniel Dunbar897ffad2009-08-31 08:09:28 +0000145void MCMachOStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
Michael J. Spencere2da0a42010-07-19 06:13:10 +0000146 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
147 // MCObjectStreamer.
Daniel Dunbar5d913472009-10-16 01:58:23 +0000148 // FIXME: Lift context changes into super class.
Daniel Dunbar8a3c9d92010-06-16 20:04:22 +0000149 getAssembler().getOrCreateSymbolData(*Symbol);
Daniel Dunbar7a989da2010-05-05 17:41:00 +0000150 Symbol->setVariableValue(AddValueSymbols(Value));
Daniel Dunbar3016db32009-08-21 09:11:24 +0000151}
152
153void MCMachOStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
Chris Lattner685508c2010-01-23 06:39:22 +0000154 MCSymbolAttr Attribute) {
Daniel Dunbar582d6102009-08-24 11:56:58 +0000155 // Indirect symbols are handled differently, to match how 'as' handles
156 // them. This makes writing matching .o files easier.
Chris Lattner685508c2010-01-23 06:39:22 +0000157 if (Attribute == MCSA_IndirectSymbol) {
Daniel Dunbarc2c0bf92009-08-26 00:18:21 +0000158 // Note that we intentionally cannot use the symbol data here; this is
159 // important for matching the string table that 'as' generates.
Daniel Dunbar582d6102009-08-24 11:56:58 +0000160 IndirectSymbolData ISD;
161 ISD.Symbol = Symbol;
Daniel Dunbarb2347fe2010-06-16 20:04:25 +0000162 ISD.SectionData = getCurrentSectionData();
Daniel Dunbar8a3c9d92010-06-16 20:04:22 +0000163 getAssembler().getIndirectSymbols().push_back(ISD);
Daniel Dunbar582d6102009-08-24 11:56:58 +0000164 return;
165 }
166
Daniel Dunbar04a11582009-08-24 08:40:12 +0000167 // Adding a symbol attribute always introduces the symbol, note that an
Daniel Dunbar6eab7212010-03-10 20:58:29 +0000168 // important side effect of calling getOrCreateSymbolData here is to register
169 // the symbol with the assembler.
Daniel Dunbar8a3c9d92010-06-16 20:04:22 +0000170 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
Daniel Dunbar04a11582009-08-24 08:40:12 +0000171
172 // The implementation of symbol attributes is designed to match 'as', but it
173 // leaves much to desired. It doesn't really make sense to arbitrarily add and
174 // remove flags, but 'as' allows this (in particular, see .desc).
175 //
176 // In the future it might be worth trying to make these operations more well
177 // defined.
Daniel Dunbard1859472009-08-22 11:41:10 +0000178 switch (Attribute) {
Chris Lattner685508c2010-01-23 06:39:22 +0000179 case MCSA_Invalid:
Chris Lattnerbc8f6382010-01-25 18:30:45 +0000180 case MCSA_ELF_TypeFunction:
181 case MCSA_ELF_TypeIndFunction:
182 case MCSA_ELF_TypeObject:
183 case MCSA_ELF_TypeTLS:
184 case MCSA_ELF_TypeCommon:
185 case MCSA_ELF_TypeNoType:
Chris Lattner685508c2010-01-23 06:39:22 +0000186 case MCSA_IndirectSymbol:
187 case MCSA_Hidden:
188 case MCSA_Internal:
189 case MCSA_Protected:
190 case MCSA_Weak:
191 case MCSA_Local:
Daniel Dunbar04a11582009-08-24 08:40:12 +0000192 assert(0 && "Invalid symbol attribute for Mach-O!");
193 break;
Daniel Dunbard1859472009-08-22 11:41:10 +0000194
Chris Lattner685508c2010-01-23 06:39:22 +0000195 case MCSA_Global:
Daniel Dunbar2701eee2009-08-28 07:08:35 +0000196 SD.setExternal(true);
Daniel Dunbar0211a962010-05-17 20:12:31 +0000197 // This effectively clears the undefined lazy bit, in Darwin 'as', although
198 // it isn't very consistent because it implements this as part of symbol
199 // lookup.
200 //
201 // FIXME: Cleanup this code, these bits should be emitted based on semantic
202 // properties, not on the order of definition, etc.
203 SD.setFlags(SD.getFlags() & ~SF_ReferenceTypeUndefinedLazy);
Daniel Dunbard1859472009-08-22 11:41:10 +0000204 break;
Daniel Dunbar04a11582009-08-24 08:40:12 +0000205
Chris Lattner685508c2010-01-23 06:39:22 +0000206 case MCSA_LazyReference:
Daniel Dunbar04a11582009-08-24 08:40:12 +0000207 // FIXME: This requires -dynamic.
208 SD.setFlags(SD.getFlags() | SF_NoDeadStrip);
209 if (Symbol->isUndefined())
210 SD.setFlags(SD.getFlags() | SF_ReferenceTypeUndefinedLazy);
211 break;
212
Daniel Dunbar04a11582009-08-24 08:40:12 +0000213 // Since .reference sets the no dead strip bit, it is equivalent to
214 // .no_dead_strip in practice.
Chris Lattner685508c2010-01-23 06:39:22 +0000215 case MCSA_Reference:
216 case MCSA_NoDeadStrip:
Daniel Dunbar04a11582009-08-24 08:40:12 +0000217 SD.setFlags(SD.getFlags() | SF_NoDeadStrip);
218 break;
219
Chris Lattner685508c2010-01-23 06:39:22 +0000220 case MCSA_PrivateExtern:
Daniel Dunbar04a11582009-08-24 08:40:12 +0000221 SD.setExternal(true);
222 SD.setPrivateExtern(true);
223 break;
224
Chris Lattner685508c2010-01-23 06:39:22 +0000225 case MCSA_WeakReference:
Daniel Dunbar04a11582009-08-24 08:40:12 +0000226 // FIXME: This requires -dynamic.
227 if (Symbol->isUndefined())
228 SD.setFlags(SD.getFlags() | SF_WeakReference);
229 break;
230
Chris Lattner685508c2010-01-23 06:39:22 +0000231 case MCSA_WeakDefinition:
Daniel Dunbar04a11582009-08-24 08:40:12 +0000232 // FIXME: 'as' enforces that this is defined and global. The manual claims
233 // it has to be in a coalesced section, but this isn't enforced.
234 SD.setFlags(SD.getFlags() | SF_WeakDefinition);
235 break;
Kevin Enderby082d0fd2010-07-08 17:22:42 +0000236
237 case MCSA_WeakDefAutoPrivate:
238 SD.setFlags(SD.getFlags() | SF_WeakDefinition | SF_WeakReference);
239 break;
Daniel Dunbard1859472009-08-22 11:41:10 +0000240 }
Daniel Dunbar3016db32009-08-21 09:11:24 +0000241}
242
243void MCMachOStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
Daniel Dunbar04a11582009-08-24 08:40:12 +0000244 // Encode the 'desc' value into the lowest implementation defined bits.
Daniel Dunbar9d40ef12010-05-26 20:37:00 +0000245 assert(DescValue == (DescValue & SF_DescFlagsMask) &&
Daniel Dunbar04a11582009-08-24 08:40:12 +0000246 "Invalid .desc value!");
Daniel Dunbar8a3c9d92010-06-16 20:04:22 +0000247 getAssembler().getOrCreateSymbolData(*Symbol).setFlags(
248 DescValue & SF_DescFlagsMask);
Daniel Dunbar3016db32009-08-21 09:11:24 +0000249}
250
Chris Lattnerb1301f72010-01-23 07:47:02 +0000251void MCMachOStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Daniel Dunbar6a715dc2009-08-30 06:17:16 +0000252 unsigned ByteAlignment) {
Daniel Dunbar2701eee2009-08-28 07:08:35 +0000253 // FIXME: Darwin 'as' does appear to allow redef of a .comm by itself.
254 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
255
Daniel Dunbar8a3c9d92010-06-16 20:04:22 +0000256 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
Daniel Dunbar2701eee2009-08-28 07:08:35 +0000257 SD.setExternal(true);
Daniel Dunbar6a715dc2009-08-30 06:17:16 +0000258 SD.setCommon(Size, ByteAlignment);
Daniel Dunbar3016db32009-08-21 09:11:24 +0000259}
260
Daniel Dunbarcf72e1c2009-08-28 05:48:22 +0000261void MCMachOStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
Daniel Dunbar6a715dc2009-08-30 06:17:16 +0000262 unsigned Size, unsigned ByteAlignment) {
Daniel Dunbar8a3c9d92010-06-16 20:04:22 +0000263 MCSectionData &SectData = getAssembler().getOrCreateSectionData(*Section);
Daniel Dunbar42a39d02009-08-28 05:49:21 +0000264
265 // The symbol may not be present, which only creates the section.
266 if (!Symbol)
267 return;
268
269 // FIXME: Assert that this section has the zerofill type.
270
271 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
272
Daniel Dunbar8a3c9d92010-06-16 20:04:22 +0000273 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
Daniel Dunbar42a39d02009-08-28 05:49:21 +0000274
Daniel Dunbar51402b72010-05-12 22:51:27 +0000275 // Emit an align fragment if necessary.
276 if (ByteAlignment != 1)
Daniel Dunbarb76df222010-05-12 22:56:23 +0000277 new MCAlignFragment(ByteAlignment, 0, 0, ByteAlignment, &SectData);
Daniel Dunbar51402b72010-05-12 22:51:27 +0000278
Daniel Dunbar4405ffc2010-05-12 22:51:38 +0000279 MCFragment *F = new MCFillFragment(0, 0, Size, &SectData);
Daniel Dunbar42a39d02009-08-28 05:49:21 +0000280 SD.setFragment(F);
281
282 Symbol->setSection(*Section);
283
284 // Update the maximum alignment on the zero fill section if necessary.
285 if (ByteAlignment > SectData.getAlignment())
286 SectData.setAlignment(ByteAlignment);
Daniel Dunbar3016db32009-08-21 09:11:24 +0000287}
288
Eric Christopher09d47032010-05-21 23:03:53 +0000289// This should always be called with the thread local bss section. Like the
290// .zerofill directive this doesn't actually switch sections on us.
Eric Christopherfeedc902010-05-18 21:26:41 +0000291void MCMachOStreamer::EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
292 uint64_t Size, unsigned ByteAlignment) {
293 EmitZerofill(Section, Symbol, Size, ByteAlignment);
294 return;
Eric Christopher9fb6bb02010-05-14 01:50:28 +0000295}
296
Chris Lattnerc35681b2010-01-19 19:46:13 +0000297void MCMachOStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {
Michael J. Spencere2da0a42010-07-19 06:13:10 +0000298 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
299 // MCObjectStreamer.
Daniel Dunbarf1f18272010-03-22 20:35:46 +0000300 getOrCreateDataFragment()->getContents().append(Data.begin(), Data.end());
Daniel Dunbar3016db32009-08-21 09:11:24 +0000301}
302
Chris Lattnerc35681b2010-01-19 19:46:13 +0000303void MCMachOStreamer::EmitValue(const MCExpr *Value, unsigned Size,
304 unsigned AddrSpace) {
Michael J. Spencere2da0a42010-07-19 06:13:10 +0000305 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
306 // MCObjectStreamer.
Daniel Dunbarf1f18272010-03-22 20:35:46 +0000307 MCDataFragment *DF = getOrCreateDataFragment();
Daniel Dunbar1a1fbb52010-02-13 09:28:22 +0000308
309 // Avoid fixups when possible.
310 int64_t AbsValue;
Daniel Dunbar5fe31292010-02-22 22:08:57 +0000311 if (AddValueSymbols(Value)->EvaluateAsAbsolute(AbsValue)) {
Daniel Dunbar1a1fbb52010-02-13 09:28:22 +0000312 // FIXME: Endianness assumption.
313 for (unsigned i = 0; i != Size; ++i)
314 DF->getContents().push_back(uint8_t(AbsValue >> (i * 8)));
315 } else {
Daniel Dunbarb34440a2010-05-26 15:18:56 +0000316 DF->addFixup(MCFixup::Create(DF->getContents().size(),
317 AddValueSymbols(Value),
318 MCFixup::getKindForSize(Size)));
Daniel Dunbar1a1fbb52010-02-13 09:28:22 +0000319 DF->getContents().resize(DF->getContents().size() + Size, 0);
320 }
Daniel Dunbar3016db32009-08-21 09:11:24 +0000321}
322
323void MCMachOStreamer::EmitValueToAlignment(unsigned ByteAlignment,
324 int64_t Value, unsigned ValueSize,
325 unsigned MaxBytesToEmit) {
Michael J. Spencere2da0a42010-07-19 06:13:10 +0000326 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
327 // MCObjectStreamer.
Daniel Dunbarb1068e42009-08-21 23:07:38 +0000328 if (MaxBytesToEmit == 0)
329 MaxBytesToEmit = ByteAlignment;
Daniel Dunbarede8e6d2010-06-16 20:04:32 +0000330 new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit,
331 getCurrentSectionData());
Kevin Enderbye83d74f2010-02-23 18:26:34 +0000332
333 // Update the maximum alignment on the current section if necessary.
Daniel Dunbarb2347fe2010-06-16 20:04:25 +0000334 if (ByteAlignment > getCurrentSectionData()->getAlignment())
335 getCurrentSectionData()->setAlignment(ByteAlignment);
Kevin Enderbye83d74f2010-02-23 18:26:34 +0000336}
337
338void MCMachOStreamer::EmitCodeAlignment(unsigned ByteAlignment,
339 unsigned MaxBytesToEmit) {
Michael J. Spencere2da0a42010-07-19 06:13:10 +0000340 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
341 // MCObjectStreamer.
Kevin Enderbye83d74f2010-02-23 18:26:34 +0000342 if (MaxBytesToEmit == 0)
343 MaxBytesToEmit = ByteAlignment;
Daniel Dunbarb76df222010-05-12 22:56:23 +0000344 MCAlignFragment *F = new MCAlignFragment(ByteAlignment, 0, 1, MaxBytesToEmit,
Daniel Dunbarb2347fe2010-06-16 20:04:25 +0000345 getCurrentSectionData());
Daniel Dunbarb76df222010-05-12 22:56:23 +0000346 F->setEmitNops(true);
Daniel Dunbar4ef7fb92009-08-21 18:29:01 +0000347
Daniel Dunbarff3eafd2009-08-22 10:13:24 +0000348 // Update the maximum alignment on the current section if necessary.
Daniel Dunbarb2347fe2010-06-16 20:04:25 +0000349 if (ByteAlignment > getCurrentSectionData()->getAlignment())
350 getCurrentSectionData()->setAlignment(ByteAlignment);
Daniel Dunbar3016db32009-08-21 09:11:24 +0000351}
352
Daniel Dunbar897ffad2009-08-31 08:09:28 +0000353void MCMachOStreamer::EmitValueToOffset(const MCExpr *Offset,
Daniel Dunbar3016db32009-08-21 09:11:24 +0000354 unsigned char Value) {
Daniel Dunbarede8e6d2010-06-16 20:04:32 +0000355 new MCOrgFragment(*Offset, Value, getCurrentSectionData());
Daniel Dunbar3016db32009-08-21 09:11:24 +0000356}
357
Daniel Dunbar9d40ef12010-05-26 20:37:00 +0000358void MCMachOStreamer::EmitInstToFragment(const MCInst &Inst) {
Daniel Dunbarb2347fe2010-06-16 20:04:25 +0000359 MCInstFragment *IF = new MCInstFragment(Inst, getCurrentSectionData());
Daniel Dunbar9d40ef12010-05-26 20:37:00 +0000360
361 // Add the fixups and data.
362 //
363 // FIXME: Revisit this design decision when relaxation is done, we may be
364 // able to get away with not storing any extra data in the MCInst.
365 SmallVector<MCFixup, 4> Fixups;
366 SmallString<256> Code;
367 raw_svector_ostream VecOS(Code);
Daniel Dunbar8a3c9d92010-06-16 20:04:22 +0000368 getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups);
Daniel Dunbar9d40ef12010-05-26 20:37:00 +0000369 VecOS.flush();
370
371 IF->getCode() = Code;
372 IF->getFixups() = Fixups;
373}
374
375void MCMachOStreamer::EmitInstToData(const MCInst &Inst) {
376 MCDataFragment *DF = getOrCreateDataFragment();
377
378 SmallVector<MCFixup, 4> Fixups;
379 SmallString<256> Code;
380 raw_svector_ostream VecOS(Code);
Daniel Dunbar8a3c9d92010-06-16 20:04:22 +0000381 getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups);
Daniel Dunbar9d40ef12010-05-26 20:37:00 +0000382 VecOS.flush();
383
384 // Add the fixups and data.
385 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
386 Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size());
387 DF->addFixup(Fixups[i]);
388 }
389 DF->getContents().append(Code.begin(), Code.end());
390}
391
Daniel Dunbar3016db32009-08-21 09:11:24 +0000392void MCMachOStreamer::EmitInstruction(const MCInst &Inst) {
Daniel Dunbarc7c5f9f2009-08-27 08:17:51 +0000393 // Scan for values.
Daniel Dunbarb7b796c2010-05-17 21:19:59 +0000394 for (unsigned i = Inst.getNumOperands(); i--; )
Daniel Dunbar73da11e2009-08-31 08:08:38 +0000395 if (Inst.getOperand(i).isExpr())
396 AddValueSymbols(Inst.getOperand(i).getExpr());
Daniel Dunbarc7c5f9f2009-08-27 08:17:51 +0000397
Daniel Dunbarb2347fe2010-06-16 20:04:25 +0000398 getCurrentSectionData()->setHasInstructions(true);
Daniel Dunbar255a8c82010-02-02 21:44:01 +0000399
Daniel Dunbarb889fc92010-05-26 20:37:03 +0000400 // If this instruction doesn't need relaxation, just emit it as data.
Daniel Dunbar8a3c9d92010-06-16 20:04:22 +0000401 if (!getAssembler().getBackend().MayNeedRelaxation(Inst)) {
Daniel Dunbar9d40ef12010-05-26 20:37:00 +0000402 EmitInstToData(Inst);
Daniel Dunbarb889fc92010-05-26 20:37:03 +0000403 return;
404 }
405
406 // Otherwise, if we are relaxing everything, relax the instruction as much as
407 // possible and emit it as data.
Daniel Dunbar8a3c9d92010-06-16 20:04:22 +0000408 if (getAssembler().getRelaxAll()) {
Daniel Dunbarb889fc92010-05-26 20:37:03 +0000409 MCInst Relaxed;
Daniel Dunbar8a3c9d92010-06-16 20:04:22 +0000410 getAssembler().getBackend().RelaxInstruction(Inst, Relaxed);
411 while (getAssembler().getBackend().MayNeedRelaxation(Relaxed))
412 getAssembler().getBackend().RelaxInstruction(Relaxed, Relaxed);
Daniel Dunbarb889fc92010-05-26 20:37:03 +0000413 EmitInstToData(Relaxed);
414 return;
415 }
416
417 // Otherwise emit to a separate fragment.
418 EmitInstToFragment(Inst);
Daniel Dunbar3016db32009-08-21 09:11:24 +0000419}
420
Daniel Dunbarede8e6d2010-06-16 20:04:32 +0000421void MCMachOStreamer::Finish() {
422 // We have to set the fragment atom associations so we can relax properly for
423 // Mach-O.
424
425 // First, scan the symbol table to build a lookup table from fragments to
426 // defining symbols.
427 DenseMap<const MCFragment*, MCSymbolData*> DefiningSymbolMap;
428 for (MCAssembler::symbol_iterator it = getAssembler().symbol_begin(),
429 ie = getAssembler().symbol_end(); it != ie; ++it) {
430 if (getAssembler().isSymbolLinkerVisible(it->getSymbol()) &&
431 it->getFragment()) {
432 // An atom defining symbol should never be internal to a fragment.
433 assert(it->getOffset() == 0 && "Invalid offset in atom defining symbol!");
434 DefiningSymbolMap[it->getFragment()] = it;
435 }
436 }
437
438 // Set the fragment atom associations by tracking the last seen atom defining
439 // symbol.
440 for (MCAssembler::iterator it = getAssembler().begin(),
441 ie = getAssembler().end(); it != ie; ++it) {
442 MCSymbolData *CurrentAtom = 0;
443 for (MCSectionData::iterator it2 = it->begin(),
444 ie2 = it->end(); it2 != ie2; ++it2) {
445 if (MCSymbolData *SD = DefiningSymbolMap.lookup(it2))
446 CurrentAtom = SD;
447 it2->setAtom(CurrentAtom);
448 }
449 }
450
451 this->MCObjectStreamer::Finish();
452}
453
Daniel Dunbarc5ddbad2010-03-11 01:34:27 +0000454MCStreamer *llvm::createMachOStreamer(MCContext &Context, TargetAsmBackend &TAB,
Daniel Dunbard821f4a2010-03-25 22:49:09 +0000455 raw_ostream &OS, MCCodeEmitter *CE,
456 bool RelaxAll) {
457 MCMachOStreamer *S = new MCMachOStreamer(Context, TAB, OS, CE);
458 if (RelaxAll)
459 S->getAssembler().setRelaxAll(true);
460 return S;
Daniel Dunbar3016db32009-08-21 09:11:24 +0000461}