blob: 9504392bc1bc97f70b044ad62144482a122ac801 [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 Dunbarfb4a6b32009-08-21 09:11:24 +000021using namespace llvm;
22
23namespace {
24
25class MCMachOStreamer : public MCStreamer {
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +000026 /// SymbolFlags - We store the value for the 'desc' symbol field in the lowest
27 /// 16 bits of the implementation defined flags.
28 enum SymbolFlags { // See <mach-o/nlist.h>.
29 SF_DescFlagsMask = 0xFFFF,
30
31 // Reference type flags.
32 SF_ReferenceTypeMask = 0x0007,
33 SF_ReferenceTypeUndefinedNonLazy = 0x0000,
34 SF_ReferenceTypeUndefinedLazy = 0x0001,
35 SF_ReferenceTypeDefined = 0x0002,
36 SF_ReferenceTypePrivateDefined = 0x0003,
37 SF_ReferenceTypePrivateUndefinedNonLazy = 0x0004,
38 SF_ReferenceTypePrivateUndefinedLazy = 0x0005,
39
40 // Other 'desc' flags.
41 SF_NoDeadStrip = 0x0020,
42 SF_WeakReference = 0x0040,
43 SF_WeakDefinition = 0x0080
44 };
45
46private:
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000047 MCAssembler Assembler;
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000048 MCSectionData *CurSectionData;
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +000049
50private:
51 MCFragment *getCurrentFragment() const {
52 assert(CurSectionData && "No current section!");
53
54 if (!CurSectionData->empty())
55 return &CurSectionData->getFragmentList().back();
56
57 return 0;
58 }
59
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000060public:
Daniel Dunbar1f3e4452010-03-11 01:34:27 +000061 MCMachOStreamer(MCContext &Context, TargetAsmBackend &TAB,
62 raw_ostream &_OS, MCCodeEmitter *_Emitter)
Daniel Dunbarcf871e52010-03-19 10:43:18 +000063 : MCStreamer(Context), Assembler(Context, TAB, *_Emitter, _OS),
Daniel Dunbar4fac7492009-08-27 08:17:51 +000064 CurSectionData(0) {}
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000065 ~MCMachOStreamer() {}
66
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +000067 const MCExpr *AddValueSymbols(const MCExpr *Value) {
68 switch (Value->getKind()) {
Chris Lattner5d917a82010-02-08 19:41:07 +000069 case MCExpr::Target: assert(0 && "Can't handle target exprs yet!");
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +000070 case MCExpr::Constant:
71 break;
72
73 case MCExpr::Binary: {
74 const MCBinaryExpr *BE = cast<MCBinaryExpr>(Value);
75 AddValueSymbols(BE->getLHS());
76 AddValueSymbols(BE->getRHS());
77 break;
78 }
79
80 case MCExpr::SymbolRef:
Daniel Dunbar46836a72010-03-10 20:58:29 +000081 Assembler.getOrCreateSymbolData(
82 cast<MCSymbolRefExpr>(Value)->getSymbol());
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +000083 break;
84
85 case MCExpr::Unary:
86 AddValueSymbols(cast<MCUnaryExpr>(Value)->getSubExpr());
87 break;
88 }
89
90 return Value;
91 }
92
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000093 /// @name MCStreamer Interface
94 /// @{
95
96 virtual void SwitchSection(const MCSection *Section);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000097 virtual void EmitLabel(MCSymbol *Symbol);
Chris Lattnera5ad93a2010-01-23 06:39:22 +000098 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag);
Daniel Dunbar821e3332009-08-31 08:09:28 +000099 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value);
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000100 virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000101 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
Chris Lattner9eb158d2010-01-23 07:47:02 +0000102 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000103 unsigned ByteAlignment);
Chris Lattner99328ad2010-01-25 07:52:13 +0000104 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
105 assert(0 && "macho doesn't support this directive");
106 }
Chris Lattner9eb158d2010-01-23 07:47:02 +0000107 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size) {
108 assert(0 && "macho doesn't support this directive");
109 }
Daniel Dunbar8751b942009-08-28 05:48:22 +0000110 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000111 unsigned Size = 0, unsigned ByteAlignment = 0);
Chris Lattneraaec2052010-01-19 19:46:13 +0000112 virtual void EmitBytes(StringRef Data, unsigned AddrSpace);
Chris Lattneraaec2052010-01-19 19:46:13 +0000113 virtual void EmitValue(const MCExpr *Value, unsigned Size,unsigned AddrSpace);
Chris Lattner718fb592010-01-25 21:28:50 +0000114 virtual void EmitGPRel32Value(const MCExpr *Value) {
115 assert(0 && "macho doesn't support this directive");
116 }
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000117 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
118 unsigned ValueSize = 1,
119 unsigned MaxBytesToEmit = 0);
Kevin Enderby6e720482010-02-23 18:26:34 +0000120 virtual void EmitCodeAlignment(unsigned ByteAlignment,
121 unsigned MaxBytesToEmit = 0);
Daniel Dunbar821e3332009-08-31 08:09:28 +0000122 virtual void EmitValueToOffset(const MCExpr *Offset,
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000123 unsigned char Value = 0);
Chris Lattnera6594fc2010-01-25 18:58:59 +0000124
125 virtual void EmitFileDirective(StringRef Filename) {
126 errs() << "FIXME: MCMachoStreamer:EmitFileDirective not implemented\n";
127 }
128 virtual void EmitDwarfFileDirective(unsigned FileNo, StringRef Filename) {
129 errs() << "FIXME: MCMachoStreamer:EmitDwarfFileDirective not implemented\n";
130 }
131
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000132 virtual void EmitInstruction(const MCInst &Inst);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000133 virtual void Finish();
134
135 /// @}
136};
137
138} // end anonymous namespace.
139
140void MCMachOStreamer::SwitchSection(const MCSection *Section) {
141 assert(Section && "Cannot switch to a null section!");
Chris Lattnercda7f782009-08-22 19:35:08 +0000142
143 // If already in this section, then this is a noop.
144 if (Section == CurSection) return;
Daniel Dunbar0f5fa692009-08-28 05:48:54 +0000145
Chris Lattnercda7f782009-08-22 19:35:08 +0000146 CurSection = Section;
Daniel Dunbar46836a72010-03-10 20:58:29 +0000147 CurSectionData = &Assembler.getOrCreateSectionData(*Section);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000148}
149
150void MCMachOStreamer::EmitLabel(MCSymbol *Symbol) {
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000151 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000152
Daniel Dunbar5e835962009-08-26 02:48:04 +0000153 // FIXME: We should also use offsets into Fill fragments.
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000154 MCDataFragment *F = dyn_cast_or_null<MCDataFragment>(getCurrentFragment());
155 if (!F)
156 F = new MCDataFragment(CurSectionData);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000157
Daniel Dunbar46836a72010-03-10 20:58:29 +0000158 MCSymbolData &SD = Assembler.getOrCreateSymbolData(*Symbol);
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000159 assert(!SD.getFragment() && "Unexpected fragment on symbol data!");
160 SD.setFragment(F);
161 SD.setOffset(F->getContents().size());
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000162
163 // This causes the reference type and weak reference flags to be cleared.
164 SD.setFlags(SD.getFlags() & ~(SF_WeakReference | SF_ReferenceTypeMask));
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000165
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000166 Symbol->setSection(*CurSection);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000167}
168
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000169void MCMachOStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
Daniel Dunbar6009db42009-08-26 21:22:22 +0000170 switch (Flag) {
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000171 case MCAF_SubsectionsViaSymbols:
Daniel Dunbar6009db42009-08-26 21:22:22 +0000172 Assembler.setSubsectionsViaSymbols(true);
Daniel Dunbar8c3eaf42009-08-28 07:08:47 +0000173 return;
Daniel Dunbar6009db42009-08-26 21:22:22 +0000174 }
Daniel Dunbar8c3eaf42009-08-28 07:08:47 +0000175
176 assert(0 && "invalid assembler flag!");
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000177}
178
Daniel Dunbar821e3332009-08-31 08:09:28 +0000179void MCMachOStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000180 // Only absolute symbols can be redefined.
181 assert((Symbol->isUndefined() || Symbol->isAbsolute()) &&
182 "Cannot define a symbol twice!");
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000183
Daniel Dunbara4d86672009-10-16 01:58:23 +0000184 // FIXME: Lift context changes into super class.
185 // FIXME: Set associated section.
Daniel Dunbard3da3622010-03-14 03:10:40 +0000186 Symbol->setValue(AddValueSymbols(Value));
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000187}
188
189void MCMachOStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000190 MCSymbolAttr Attribute) {
Daniel Dunbar0c7761b2009-08-24 11:56:58 +0000191 // Indirect symbols are handled differently, to match how 'as' handles
192 // them. This makes writing matching .o files easier.
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000193 if (Attribute == MCSA_IndirectSymbol) {
Daniel Dunbarad7c3d52009-08-26 00:18:21 +0000194 // Note that we intentionally cannot use the symbol data here; this is
195 // important for matching the string table that 'as' generates.
Daniel Dunbar0c7761b2009-08-24 11:56:58 +0000196 IndirectSymbolData ISD;
197 ISD.Symbol = Symbol;
198 ISD.SectionData = CurSectionData;
199 Assembler.getIndirectSymbols().push_back(ISD);
200 return;
201 }
202
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000203 // Adding a symbol attribute always introduces the symbol, note that an
Daniel Dunbar46836a72010-03-10 20:58:29 +0000204 // important side effect of calling getOrCreateSymbolData here is to register
205 // the symbol with the assembler.
206 MCSymbolData &SD = Assembler.getOrCreateSymbolData(*Symbol);
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000207
208 // The implementation of symbol attributes is designed to match 'as', but it
209 // leaves much to desired. It doesn't really make sense to arbitrarily add and
210 // remove flags, but 'as' allows this (in particular, see .desc).
211 //
212 // In the future it might be worth trying to make these operations more well
213 // defined.
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000214 switch (Attribute) {
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000215 case MCSA_Invalid:
Chris Lattnered0ab152010-01-25 18:30:45 +0000216 case MCSA_ELF_TypeFunction:
217 case MCSA_ELF_TypeIndFunction:
218 case MCSA_ELF_TypeObject:
219 case MCSA_ELF_TypeTLS:
220 case MCSA_ELF_TypeCommon:
221 case MCSA_ELF_TypeNoType:
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000222 case MCSA_IndirectSymbol:
223 case MCSA_Hidden:
224 case MCSA_Internal:
225 case MCSA_Protected:
226 case MCSA_Weak:
227 case MCSA_Local:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000228 assert(0 && "Invalid symbol attribute for Mach-O!");
229 break;
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000230
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000231 case MCSA_Global:
Daniel Dunbar8f4d1462009-08-28 07:08:35 +0000232 SD.setExternal(true);
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000233 break;
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000234
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000235 case MCSA_LazyReference:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000236 // FIXME: This requires -dynamic.
237 SD.setFlags(SD.getFlags() | SF_NoDeadStrip);
238 if (Symbol->isUndefined())
239 SD.setFlags(SD.getFlags() | SF_ReferenceTypeUndefinedLazy);
240 break;
241
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000242 // Since .reference sets the no dead strip bit, it is equivalent to
243 // .no_dead_strip in practice.
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000244 case MCSA_Reference:
245 case MCSA_NoDeadStrip:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000246 SD.setFlags(SD.getFlags() | SF_NoDeadStrip);
247 break;
248
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000249 case MCSA_PrivateExtern:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000250 SD.setExternal(true);
251 SD.setPrivateExtern(true);
252 break;
253
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000254 case MCSA_WeakReference:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000255 // FIXME: This requires -dynamic.
256 if (Symbol->isUndefined())
257 SD.setFlags(SD.getFlags() | SF_WeakReference);
258 break;
259
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000260 case MCSA_WeakDefinition:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000261 // FIXME: 'as' enforces that this is defined and global. The manual claims
262 // it has to be in a coalesced section, but this isn't enforced.
263 SD.setFlags(SD.getFlags() | SF_WeakDefinition);
264 break;
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000265 }
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000266}
267
268void MCMachOStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000269 // Encode the 'desc' value into the lowest implementation defined bits.
270 assert(DescValue == (DescValue & SF_DescFlagsMask) &&
271 "Invalid .desc value!");
Daniel Dunbar46836a72010-03-10 20:58:29 +0000272 Assembler.getOrCreateSymbolData(*Symbol).setFlags(DescValue&SF_DescFlagsMask);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000273}
274
Chris Lattner9eb158d2010-01-23 07:47:02 +0000275void MCMachOStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000276 unsigned ByteAlignment) {
Daniel Dunbar8f4d1462009-08-28 07:08:35 +0000277 // FIXME: Darwin 'as' does appear to allow redef of a .comm by itself.
278 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
279
Daniel Dunbar46836a72010-03-10 20:58:29 +0000280 MCSymbolData &SD = Assembler.getOrCreateSymbolData(*Symbol);
Daniel Dunbar8f4d1462009-08-28 07:08:35 +0000281 SD.setExternal(true);
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000282 SD.setCommon(Size, ByteAlignment);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000283}
284
Daniel Dunbar8751b942009-08-28 05:48:22 +0000285void MCMachOStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000286 unsigned Size, unsigned ByteAlignment) {
Daniel Dunbar46836a72010-03-10 20:58:29 +0000287 MCSectionData &SectData = Assembler.getOrCreateSectionData(*Section);
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000288
289 // The symbol may not be present, which only creates the section.
290 if (!Symbol)
291 return;
292
293 // FIXME: Assert that this section has the zerofill type.
294
295 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
296
Daniel Dunbar46836a72010-03-10 20:58:29 +0000297 MCSymbolData &SD = Assembler.getOrCreateSymbolData(*Symbol);
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000298
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000299 MCFragment *F = new MCZeroFillFragment(Size, ByteAlignment, &SectData);
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000300 SD.setFragment(F);
301
302 Symbol->setSection(*Section);
303
304 // Update the maximum alignment on the zero fill section if necessary.
305 if (ByteAlignment > SectData.getAlignment())
306 SectData.setAlignment(ByteAlignment);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000307}
308
Chris Lattneraaec2052010-01-19 19:46:13 +0000309void MCMachOStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000310 MCDataFragment *DF = dyn_cast_or_null<MCDataFragment>(getCurrentFragment());
311 if (!DF)
312 DF = new MCDataFragment(CurSectionData);
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000313 DF->getContents().append(Data.begin(), Data.end());
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000314}
315
Chris Lattneraaec2052010-01-19 19:46:13 +0000316void MCMachOStreamer::EmitValue(const MCExpr *Value, unsigned Size,
317 unsigned AddrSpace) {
Daniel Dunbar45f48742010-02-13 09:28:22 +0000318 MCDataFragment *DF = dyn_cast_or_null<MCDataFragment>(getCurrentFragment());
319 if (!DF)
320 DF = new MCDataFragment(CurSectionData);
321
322 // Avoid fixups when possible.
323 int64_t AbsValue;
Daniel Dunbar40ebe2472010-02-22 22:08:57 +0000324 if (AddValueSymbols(Value)->EvaluateAsAbsolute(AbsValue)) {
Daniel Dunbar45f48742010-02-13 09:28:22 +0000325 // FIXME: Endianness assumption.
326 for (unsigned i = 0; i != Size; ++i)
327 DF->getContents().push_back(uint8_t(AbsValue >> (i * 8)));
328 } else {
Daniel Dunbar8315a352010-03-12 21:00:38 +0000329 DF->addFixup(MCAsmFixup(DF->getContents().size(), *AddValueSymbols(Value),
330 MCFixup::getKindForSize(Size)));
Daniel Dunbar45f48742010-02-13 09:28:22 +0000331 DF->getContents().resize(DF->getContents().size() + Size, 0);
332 }
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000333}
334
335void MCMachOStreamer::EmitValueToAlignment(unsigned ByteAlignment,
336 int64_t Value, unsigned ValueSize,
337 unsigned MaxBytesToEmit) {
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000338 if (MaxBytesToEmit == 0)
339 MaxBytesToEmit = ByteAlignment;
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000340 new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit,
Kevin Enderby6e720482010-02-23 18:26:34 +0000341 false /* EmitNops */, CurSectionData);
342
343 // Update the maximum alignment on the current section if necessary.
344 if (ByteAlignment > CurSectionData->getAlignment())
345 CurSectionData->setAlignment(ByteAlignment);
346}
347
348void MCMachOStreamer::EmitCodeAlignment(unsigned ByteAlignment,
349 unsigned MaxBytesToEmit) {
350 if (MaxBytesToEmit == 0)
351 MaxBytesToEmit = ByteAlignment;
352 // FIXME the 0x90 is the default x86 1 byte nop opcode.
353 new MCAlignFragment(ByteAlignment, 0x90, 1, MaxBytesToEmit,
354 true /* EmitNops */, CurSectionData);
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000355
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000356 // Update the maximum alignment on the current section if necessary.
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000357 if (ByteAlignment > CurSectionData->getAlignment())
358 CurSectionData->setAlignment(ByteAlignment);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000359}
360
Daniel Dunbar821e3332009-08-31 08:09:28 +0000361void MCMachOStreamer::EmitValueToOffset(const MCExpr *Offset,
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000362 unsigned char Value) {
Daniel Dunbar1253a6f2009-10-16 01:58:03 +0000363 new MCOrgFragment(*Offset, Value, CurSectionData);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000364}
365
366void MCMachOStreamer::EmitInstruction(const MCInst &Inst) {
Daniel Dunbar4fac7492009-08-27 08:17:51 +0000367 // Scan for values.
368 for (unsigned i = 0; i != Inst.getNumOperands(); ++i)
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000369 if (Inst.getOperand(i).isExpr())
370 AddValueSymbols(Inst.getOperand(i).getExpr());
Daniel Dunbar4fac7492009-08-27 08:17:51 +0000371
Daniel Dunbare1ec6172010-02-02 21:44:01 +0000372 CurSectionData->setHasInstructions(true);
373
Daniel Dunbar73c55742010-02-09 22:59:55 +0000374 SmallVector<MCFixup, 4> Fixups;
Daniel Dunbar4fac7492009-08-27 08:17:51 +0000375 SmallString<256> Code;
376 raw_svector_ostream VecOS(Code);
Daniel Dunbarcf871e52010-03-19 10:43:18 +0000377 Assembler.getEmitter().EncodeInstruction(Inst, VecOS, Fixups);
Daniel Dunbarf6346762010-02-13 09:29:02 +0000378 VecOS.flush();
379
380 // Add the fixups and data.
381 MCDataFragment *DF = dyn_cast_or_null<MCDataFragment>(getCurrentFragment());
382 if (!DF)
383 DF = new MCDataFragment(CurSectionData);
384 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
385 MCFixup &F = Fixups[i];
Daniel Dunbar8315a352010-03-12 21:00:38 +0000386 DF->addFixup(MCAsmFixup(DF->getContents().size()+F.getOffset(),
387 *F.getValue(), F.getKind()));
Daniel Dunbarf6346762010-02-13 09:29:02 +0000388 }
389 DF->getContents().append(Code.begin(), Code.end());
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000390}
391
392void MCMachOStreamer::Finish() {
393 Assembler.Finish();
394}
395
Daniel Dunbar1f3e4452010-03-11 01:34:27 +0000396MCStreamer *llvm::createMachOStreamer(MCContext &Context, TargetAsmBackend &TAB,
397 raw_ostream &OS, MCCodeEmitter *CE) {
398 return new MCMachOStreamer(Context, TAB, OS, CE);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000399}