blob: 5ca31943f1f7485ab700aae161397db49c16425f [file] [log] [blame]
Daniel Dunbarfb4a6b32009-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 Dunbarfb4a6b32009-08-21 09:11:24 +000012#include "llvm/MC/MCAssembler.h"
13#include "llvm/MC/MCContext.h"
Daniel Dunbar4fac7492009-08-27 08:17:51 +000014#include "llvm/MC/MCCodeEmitter.h"
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +000015#include "llvm/MC/MCExpr.h"
Daniel Dunbar4fac7492009-08-27 08:17:51 +000016#include "llvm/MC/MCInst.h"
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000017#include "llvm/MC/MCSection.h"
18#include "llvm/MC/MCSymbol.h"
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000019#include "llvm/Support/ErrorHandling.h"
Daniel Dunbar4fac7492009-08-27 08:17:51 +000020#include "llvm/Support/raw_ostream.h"
Daniel Dunbard8036fb2010-03-23 05:09:03 +000021#include "llvm/Target/TargetAsmBackend.h"
22
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000023using namespace llvm;
24
25namespace {
26
27class MCMachOStreamer : public MCStreamer {
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +000028 /// SymbolFlags - We store the value for the 'desc' symbol field in the lowest
29 /// 16 bits of the implementation defined flags.
30 enum SymbolFlags { // See <mach-o/nlist.h>.
31 SF_DescFlagsMask = 0xFFFF,
32
33 // Reference type flags.
34 SF_ReferenceTypeMask = 0x0007,
35 SF_ReferenceTypeUndefinedNonLazy = 0x0000,
36 SF_ReferenceTypeUndefinedLazy = 0x0001,
37 SF_ReferenceTypeDefined = 0x0002,
38 SF_ReferenceTypePrivateDefined = 0x0003,
39 SF_ReferenceTypePrivateUndefinedNonLazy = 0x0004,
40 SF_ReferenceTypePrivateUndefinedLazy = 0x0005,
41
42 // Other 'desc' flags.
43 SF_NoDeadStrip = 0x0020,
44 SF_WeakReference = 0x0040,
45 SF_WeakDefinition = 0x0080
46 };
47
48private:
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000049 MCAssembler Assembler;
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000050 MCSectionData *CurSectionData;
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +000051
52private:
53 MCFragment *getCurrentFragment() const {
54 assert(CurSectionData && "No current section!");
55
56 if (!CurSectionData->empty())
57 return &CurSectionData->getFragmentList().back();
58
59 return 0;
60 }
61
Daniel Dunbarf70f4772010-03-22 20:35:46 +000062 /// Get a data fragment to write into, creating a new one if the current
63 /// fragment is not a data fragment.
64 MCDataFragment *getOrCreateDataFragment() const {
65 MCDataFragment *F = dyn_cast_or_null<MCDataFragment>(getCurrentFragment());
66 if (!F)
67 F = new MCDataFragment(CurSectionData);
68 return F;
69 }
70
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000071public:
Daniel Dunbar1f3e4452010-03-11 01:34:27 +000072 MCMachOStreamer(MCContext &Context, TargetAsmBackend &TAB,
73 raw_ostream &_OS, MCCodeEmitter *_Emitter)
Daniel Dunbarcf871e52010-03-19 10:43:18 +000074 : MCStreamer(Context), Assembler(Context, TAB, *_Emitter, _OS),
Daniel Dunbar4fac7492009-08-27 08:17:51 +000075 CurSectionData(0) {}
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000076 ~MCMachOStreamer() {}
77
Daniel Dunbarac2884a2010-03-25 22:49:09 +000078 MCAssembler &getAssembler() { return Assembler; }
79
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +000080 const MCExpr *AddValueSymbols(const MCExpr *Value) {
81 switch (Value->getKind()) {
Chris Lattner5d917a82010-02-08 19:41:07 +000082 case MCExpr::Target: assert(0 && "Can't handle target exprs yet!");
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +000083 case MCExpr::Constant:
84 break;
85
86 case MCExpr::Binary: {
87 const MCBinaryExpr *BE = cast<MCBinaryExpr>(Value);
88 AddValueSymbols(BE->getLHS());
89 AddValueSymbols(BE->getRHS());
90 break;
91 }
92
93 case MCExpr::SymbolRef:
Daniel Dunbar46836a72010-03-10 20:58:29 +000094 Assembler.getOrCreateSymbolData(
95 cast<MCSymbolRefExpr>(Value)->getSymbol());
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +000096 break;
97
98 case MCExpr::Unary:
99 AddValueSymbols(cast<MCUnaryExpr>(Value)->getSubExpr());
100 break;
101 }
102
103 return Value;
104 }
105
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000106 /// @name MCStreamer Interface
107 /// @{
108
109 virtual void SwitchSection(const MCSection *Section);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000110 virtual void EmitLabel(MCSymbol *Symbol);
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000111 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag);
Daniel Dunbar821e3332009-08-31 08:09:28 +0000112 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value);
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000113 virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000114 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
Chris Lattner9eb158d2010-01-23 07:47:02 +0000115 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000116 unsigned ByteAlignment);
Chris Lattner99328ad2010-01-25 07:52:13 +0000117 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
118 assert(0 && "macho doesn't support this directive");
119 }
Chris Lattner9eb158d2010-01-23 07:47:02 +0000120 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size) {
121 assert(0 && "macho doesn't support this directive");
122 }
Daniel Dunbar8751b942009-08-28 05:48:22 +0000123 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000124 unsigned Size = 0, unsigned ByteAlignment = 0);
Chris Lattneraaec2052010-01-19 19:46:13 +0000125 virtual void EmitBytes(StringRef Data, unsigned AddrSpace);
Chris Lattneraaec2052010-01-19 19:46:13 +0000126 virtual void EmitValue(const MCExpr *Value, unsigned Size,unsigned AddrSpace);
Chris Lattner718fb592010-01-25 21:28:50 +0000127 virtual void EmitGPRel32Value(const MCExpr *Value) {
128 assert(0 && "macho doesn't support this directive");
129 }
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000130 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
131 unsigned ValueSize = 1,
132 unsigned MaxBytesToEmit = 0);
Kevin Enderby6e720482010-02-23 18:26:34 +0000133 virtual void EmitCodeAlignment(unsigned ByteAlignment,
134 unsigned MaxBytesToEmit = 0);
Daniel Dunbar821e3332009-08-31 08:09:28 +0000135 virtual void EmitValueToOffset(const MCExpr *Offset,
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000136 unsigned char Value = 0);
Chris Lattnera6594fc2010-01-25 18:58:59 +0000137
138 virtual void EmitFileDirective(StringRef Filename) {
139 errs() << "FIXME: MCMachoStreamer:EmitFileDirective not implemented\n";
140 }
141 virtual void EmitDwarfFileDirective(unsigned FileNo, StringRef Filename) {
142 errs() << "FIXME: MCMachoStreamer:EmitDwarfFileDirective not implemented\n";
143 }
144
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000145 virtual void EmitInstruction(const MCInst &Inst);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000146 virtual void Finish();
147
148 /// @}
149};
150
151} // end anonymous namespace.
152
153void MCMachOStreamer::SwitchSection(const MCSection *Section) {
154 assert(Section && "Cannot switch to a null section!");
Chris Lattnercda7f782009-08-22 19:35:08 +0000155
156 // If already in this section, then this is a noop.
157 if (Section == CurSection) return;
Daniel Dunbar0f5fa692009-08-28 05:48:54 +0000158
Chris Lattnercda7f782009-08-22 19:35:08 +0000159 CurSection = Section;
Daniel Dunbar46836a72010-03-10 20:58:29 +0000160 CurSectionData = &Assembler.getOrCreateSectionData(*Section);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000161}
162
163void MCMachOStreamer::EmitLabel(MCSymbol *Symbol) {
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000164 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
Daniel Dunbarc3047182010-05-05 19:01:00 +0000165 assert(!Symbol->isVariable() && "Cannot emit a variable symbol!");
166 assert(CurSection && "Cannot emit before setting section!");
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000167
Daniel Dunbarf70f4772010-03-22 20:35:46 +0000168 // FIXME: This is wasteful, we don't necessarily need to create a data
169 // fragment. Instead, we should mark the symbol as pointing into the data
170 // fragment if it exists, otherwise we should just queue the label and set its
171 // fragment pointer when we emit the next fragment.
172 MCDataFragment *F = getOrCreateDataFragment();
Daniel Dunbar46836a72010-03-10 20:58:29 +0000173 MCSymbolData &SD = Assembler.getOrCreateSymbolData(*Symbol);
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000174 assert(!SD.getFragment() && "Unexpected fragment on symbol data!");
175 SD.setFragment(F);
176 SD.setOffset(F->getContents().size());
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000177
178 // This causes the reference type and weak reference flags to be cleared.
179 SD.setFlags(SD.getFlags() & ~(SF_WeakReference | SF_ReferenceTypeMask));
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000180
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000181 Symbol->setSection(*CurSection);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000182}
183
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000184void MCMachOStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
Daniel Dunbar6009db42009-08-26 21:22:22 +0000185 switch (Flag) {
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000186 case MCAF_SubsectionsViaSymbols:
Daniel Dunbar6009db42009-08-26 21:22:22 +0000187 Assembler.setSubsectionsViaSymbols(true);
Daniel Dunbar8c3eaf42009-08-28 07:08:47 +0000188 return;
Daniel Dunbar6009db42009-08-26 21:22:22 +0000189 }
Daniel Dunbar8c3eaf42009-08-28 07:08:47 +0000190
191 assert(0 && "invalid assembler flag!");
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000192}
193
Daniel Dunbar821e3332009-08-31 08:09:28 +0000194void MCMachOStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
Daniel Dunbara4d86672009-10-16 01:58:23 +0000195 // FIXME: Lift context changes into super class.
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000196 Symbol->setVariableValue(AddValueSymbols(Value));
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000197}
198
199void MCMachOStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000200 MCSymbolAttr Attribute) {
Daniel Dunbar0c7761b2009-08-24 11:56:58 +0000201 // Indirect symbols are handled differently, to match how 'as' handles
202 // them. This makes writing matching .o files easier.
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000203 if (Attribute == MCSA_IndirectSymbol) {
Daniel Dunbarad7c3d52009-08-26 00:18:21 +0000204 // Note that we intentionally cannot use the symbol data here; this is
205 // important for matching the string table that 'as' generates.
Daniel Dunbar0c7761b2009-08-24 11:56:58 +0000206 IndirectSymbolData ISD;
207 ISD.Symbol = Symbol;
208 ISD.SectionData = CurSectionData;
209 Assembler.getIndirectSymbols().push_back(ISD);
210 return;
211 }
212
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000213 // Adding a symbol attribute always introduces the symbol, note that an
Daniel Dunbar46836a72010-03-10 20:58:29 +0000214 // important side effect of calling getOrCreateSymbolData here is to register
215 // the symbol with the assembler.
216 MCSymbolData &SD = Assembler.getOrCreateSymbolData(*Symbol);
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000217
218 // The implementation of symbol attributes is designed to match 'as', but it
219 // leaves much to desired. It doesn't really make sense to arbitrarily add and
220 // remove flags, but 'as' allows this (in particular, see .desc).
221 //
222 // In the future it might be worth trying to make these operations more well
223 // defined.
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000224 switch (Attribute) {
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000225 case MCSA_Invalid:
Chris Lattnered0ab152010-01-25 18:30:45 +0000226 case MCSA_ELF_TypeFunction:
227 case MCSA_ELF_TypeIndFunction:
228 case MCSA_ELF_TypeObject:
229 case MCSA_ELF_TypeTLS:
230 case MCSA_ELF_TypeCommon:
231 case MCSA_ELF_TypeNoType:
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000232 case MCSA_IndirectSymbol:
233 case MCSA_Hidden:
234 case MCSA_Internal:
235 case MCSA_Protected:
236 case MCSA_Weak:
237 case MCSA_Local:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000238 assert(0 && "Invalid symbol attribute for Mach-O!");
239 break;
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000240
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000241 case MCSA_Global:
Daniel Dunbar8f4d1462009-08-28 07:08:35 +0000242 SD.setExternal(true);
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000243 break;
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000244
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000245 case MCSA_LazyReference:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000246 // FIXME: This requires -dynamic.
247 SD.setFlags(SD.getFlags() | SF_NoDeadStrip);
248 if (Symbol->isUndefined())
249 SD.setFlags(SD.getFlags() | SF_ReferenceTypeUndefinedLazy);
250 break;
251
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000252 // Since .reference sets the no dead strip bit, it is equivalent to
253 // .no_dead_strip in practice.
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000254 case MCSA_Reference:
255 case MCSA_NoDeadStrip:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000256 SD.setFlags(SD.getFlags() | SF_NoDeadStrip);
257 break;
258
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000259 case MCSA_PrivateExtern:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000260 SD.setExternal(true);
261 SD.setPrivateExtern(true);
262 break;
263
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000264 case MCSA_WeakReference:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000265 // FIXME: This requires -dynamic.
266 if (Symbol->isUndefined())
267 SD.setFlags(SD.getFlags() | SF_WeakReference);
268 break;
269
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000270 case MCSA_WeakDefinition:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000271 // FIXME: 'as' enforces that this is defined and global. The manual claims
272 // it has to be in a coalesced section, but this isn't enforced.
273 SD.setFlags(SD.getFlags() | SF_WeakDefinition);
274 break;
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000275 }
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000276}
277
278void MCMachOStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000279 // Encode the 'desc' value into the lowest implementation defined bits.
280 assert(DescValue == (DescValue & SF_DescFlagsMask) &&
281 "Invalid .desc value!");
Daniel Dunbar46836a72010-03-10 20:58:29 +0000282 Assembler.getOrCreateSymbolData(*Symbol).setFlags(DescValue&SF_DescFlagsMask);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000283}
284
Chris Lattner9eb158d2010-01-23 07:47:02 +0000285void MCMachOStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000286 unsigned ByteAlignment) {
Daniel Dunbar8f4d1462009-08-28 07:08:35 +0000287 // FIXME: Darwin 'as' does appear to allow redef of a .comm by itself.
288 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
289
Daniel Dunbar46836a72010-03-10 20:58:29 +0000290 MCSymbolData &SD = Assembler.getOrCreateSymbolData(*Symbol);
Daniel Dunbar8f4d1462009-08-28 07:08:35 +0000291 SD.setExternal(true);
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000292 SD.setCommon(Size, ByteAlignment);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000293}
294
Daniel Dunbar8751b942009-08-28 05:48:22 +0000295void MCMachOStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000296 unsigned Size, unsigned ByteAlignment) {
Daniel Dunbar46836a72010-03-10 20:58:29 +0000297 MCSectionData &SectData = Assembler.getOrCreateSectionData(*Section);
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000298
299 // The symbol may not be present, which only creates the section.
300 if (!Symbol)
301 return;
302
303 // FIXME: Assert that this section has the zerofill type.
304
305 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
306
Daniel Dunbar46836a72010-03-10 20:58:29 +0000307 MCSymbolData &SD = Assembler.getOrCreateSymbolData(*Symbol);
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000308
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000309 MCFragment *F = new MCZeroFillFragment(Size, ByteAlignment, &SectData);
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000310 SD.setFragment(F);
311
312 Symbol->setSection(*Section);
313
314 // Update the maximum alignment on the zero fill section if necessary.
315 if (ByteAlignment > SectData.getAlignment())
316 SectData.setAlignment(ByteAlignment);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000317}
318
Chris Lattneraaec2052010-01-19 19:46:13 +0000319void MCMachOStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {
Daniel Dunbarf70f4772010-03-22 20:35:46 +0000320 getOrCreateDataFragment()->getContents().append(Data.begin(), Data.end());
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000321}
322
Chris Lattneraaec2052010-01-19 19:46:13 +0000323void MCMachOStreamer::EmitValue(const MCExpr *Value, unsigned Size,
324 unsigned AddrSpace) {
Daniel Dunbarf70f4772010-03-22 20:35:46 +0000325 MCDataFragment *DF = getOrCreateDataFragment();
Daniel Dunbar45f48742010-02-13 09:28:22 +0000326
327 // Avoid fixups when possible.
328 int64_t AbsValue;
Daniel Dunbar40ebe2472010-02-22 22:08:57 +0000329 if (AddValueSymbols(Value)->EvaluateAsAbsolute(AbsValue)) {
Daniel Dunbar45f48742010-02-13 09:28:22 +0000330 // FIXME: Endianness assumption.
331 for (unsigned i = 0; i != Size; ++i)
332 DF->getContents().push_back(uint8_t(AbsValue >> (i * 8)));
333 } else {
Daniel Dunbar8315a352010-03-12 21:00:38 +0000334 DF->addFixup(MCAsmFixup(DF->getContents().size(), *AddValueSymbols(Value),
335 MCFixup::getKindForSize(Size)));
Daniel Dunbar45f48742010-02-13 09:28:22 +0000336 DF->getContents().resize(DF->getContents().size() + Size, 0);
337 }
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000338}
339
340void MCMachOStreamer::EmitValueToAlignment(unsigned ByteAlignment,
341 int64_t Value, unsigned ValueSize,
342 unsigned MaxBytesToEmit) {
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000343 if (MaxBytesToEmit == 0)
344 MaxBytesToEmit = ByteAlignment;
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000345 new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit,
Kevin Enderby6e720482010-02-23 18:26:34 +0000346 false /* EmitNops */, CurSectionData);
347
348 // Update the maximum alignment on the current section if necessary.
349 if (ByteAlignment > CurSectionData->getAlignment())
350 CurSectionData->setAlignment(ByteAlignment);
351}
352
353void MCMachOStreamer::EmitCodeAlignment(unsigned ByteAlignment,
354 unsigned MaxBytesToEmit) {
355 if (MaxBytesToEmit == 0)
356 MaxBytesToEmit = ByteAlignment;
Daniel Dunbar8f9b80e2010-03-23 02:36:58 +0000357 new MCAlignFragment(ByteAlignment, 0, 1, MaxBytesToEmit,
Kevin Enderby6e720482010-02-23 18:26:34 +0000358 true /* EmitNops */, CurSectionData);
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000359
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000360 // Update the maximum alignment on the current section if necessary.
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000361 if (ByteAlignment > CurSectionData->getAlignment())
362 CurSectionData->setAlignment(ByteAlignment);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000363}
364
Daniel Dunbar821e3332009-08-31 08:09:28 +0000365void MCMachOStreamer::EmitValueToOffset(const MCExpr *Offset,
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000366 unsigned char Value) {
Daniel Dunbar1253a6f2009-10-16 01:58:03 +0000367 new MCOrgFragment(*Offset, Value, CurSectionData);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000368}
369
370void MCMachOStreamer::EmitInstruction(const MCInst &Inst) {
Daniel Dunbar4fac7492009-08-27 08:17:51 +0000371 // Scan for values.
372 for (unsigned i = 0; i != Inst.getNumOperands(); ++i)
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000373 if (Inst.getOperand(i).isExpr())
374 AddValueSymbols(Inst.getOperand(i).getExpr());
Daniel Dunbar4fac7492009-08-27 08:17:51 +0000375
Daniel Dunbare1ec6172010-02-02 21:44:01 +0000376 CurSectionData->setHasInstructions(true);
377
Daniel Dunbar3f4dcd92010-03-22 23:16:48 +0000378 // FIXME-PERF: Common case is that we don't need to relax, encode directly
379 // onto the data fragments buffers.
380
Daniel Dunbar73c55742010-02-09 22:59:55 +0000381 SmallVector<MCFixup, 4> Fixups;
Daniel Dunbar4fac7492009-08-27 08:17:51 +0000382 SmallString<256> Code;
383 raw_svector_ostream VecOS(Code);
Daniel Dunbarcf871e52010-03-19 10:43:18 +0000384 Assembler.getEmitter().EncodeInstruction(Inst, VecOS, Fixups);
Daniel Dunbarf6346762010-02-13 09:29:02 +0000385 VecOS.flush();
386
Daniel Dunbar337055e2010-03-23 03:13:05 +0000387 // FIXME: Eliminate this copy.
388 SmallVector<MCAsmFixup, 4> AsmFixups;
Daniel Dunbarf6346762010-02-13 09:29:02 +0000389 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
390 MCFixup &F = Fixups[i];
Daniel Dunbar337055e2010-03-23 03:13:05 +0000391 AsmFixups.push_back(MCAsmFixup(F.getOffset(), *F.getValue(),
392 F.getKind()));
393 }
394
Daniel Dunbard8036fb2010-03-23 05:09:03 +0000395 // See if we might need to relax this instruction, if so it needs its own
396 // fragment.
397 //
398 // FIXME-PERF: Support target hook to do a fast path that avoids the encoder,
399 // when we can immediately tell that we will get something which might need
400 // relaxation (and compute its size).
401 //
402 // FIXME-PERF: We should also be smart about immediately relaxing instructions
403 // which we can already show will never possibly fit (we can also do a very
404 // good job of this before we do the first relaxation pass, because we have
405 // total knowledge about undefined symbols at that point). Even now, though,
406 // we can do a decent job, especially on Darwin where scattering means that we
407 // are going to often know that we can never fully resolve a fixup.
408 if (Assembler.getBackend().MayNeedRelaxation(Inst, AsmFixups)) {
409 MCInstFragment *IF = new MCInstFragment(Inst, CurSectionData);
410
411 // Add the fixups and data.
412 //
413 // FIXME: Revisit this design decision when relaxation is done, we may be
414 // able to get away with not storing any extra data in the MCInst.
415 IF->getCode() = Code;
416 IF->getFixups() = AsmFixups;
417
418 return;
419 }
420
Daniel Dunbar337055e2010-03-23 03:13:05 +0000421 // Add the fixups and data.
422 MCDataFragment *DF = getOrCreateDataFragment();
423 for (unsigned i = 0, e = AsmFixups.size(); i != e; ++i) {
424 AsmFixups[i].Offset += DF->getContents().size();
425 DF->addFixup(AsmFixups[i]);
Daniel Dunbarf6346762010-02-13 09:29:02 +0000426 }
427 DF->getContents().append(Code.begin(), Code.end());
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000428}
429
430void MCMachOStreamer::Finish() {
431 Assembler.Finish();
432}
433
Daniel Dunbar1f3e4452010-03-11 01:34:27 +0000434MCStreamer *llvm::createMachOStreamer(MCContext &Context, TargetAsmBackend &TAB,
Daniel Dunbarac2884a2010-03-25 22:49:09 +0000435 raw_ostream &OS, MCCodeEmitter *CE,
436 bool RelaxAll) {
437 MCMachOStreamer *S = new MCMachOStreamer(Context, TAB, OS, CE);
438 if (RelaxAll)
439 S->getAssembler().setRelaxAll(true);
440 return S;
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000441}