blob: 9f62efee2f977718f0b55f6a19715cc236d248b5 [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 Dunbar4fac7492009-08-27 08:17:51 +000048 MCCodeEmitter *Emitter;
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000049 MCSectionData *CurSectionData;
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000050 DenseMap<const MCSection*, MCSectionData*> SectionMap;
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +000051 DenseMap<const MCSymbol*, MCSymbolData*> SymbolMap;
52
53private:
54 MCFragment *getCurrentFragment() const {
55 assert(CurSectionData && "No current section!");
56
57 if (!CurSectionData->empty())
58 return &CurSectionData->getFragmentList().back();
59
60 return 0;
61 }
62
Daniel Dunbar0f5fa692009-08-28 05:48:54 +000063 MCSectionData &getSectionData(const MCSection &Section) {
64 MCSectionData *&Entry = SectionMap[&Section];
65
66 if (!Entry)
67 Entry = new MCSectionData(Section, &Assembler);
68
69 return *Entry;
70 }
71
Daniel Dunbarcb579b32009-08-31 08:08:06 +000072 MCSymbolData &getSymbolData(const MCSymbol &Symbol) {
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +000073 MCSymbolData *&Entry = SymbolMap[&Symbol];
74
75 if (!Entry)
76 Entry = new MCSymbolData(Symbol, 0, 0, &Assembler);
77
78 return *Entry;
79 }
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000080
81public:
Daniel Dunbar4fac7492009-08-27 08:17:51 +000082 MCMachOStreamer(MCContext &Context, raw_ostream &_OS, MCCodeEmitter *_Emitter)
Daniel Dunbara03a3682009-08-31 08:07:55 +000083 : MCStreamer(Context), Assembler(Context, _OS), Emitter(_Emitter),
Daniel Dunbar4fac7492009-08-27 08:17:51 +000084 CurSectionData(0) {}
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000085 ~MCMachOStreamer() {}
86
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +000087 const MCExpr *AddValueSymbols(const MCExpr *Value) {
88 switch (Value->getKind()) {
Chris Lattner5d917a82010-02-08 19:41:07 +000089 case MCExpr::Target: assert(0 && "Can't handle target exprs yet!");
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +000090 case MCExpr::Constant:
91 break;
92
93 case MCExpr::Binary: {
94 const MCBinaryExpr *BE = cast<MCBinaryExpr>(Value);
95 AddValueSymbols(BE->getLHS());
96 AddValueSymbols(BE->getRHS());
97 break;
98 }
99
100 case MCExpr::SymbolRef:
101 getSymbolData(cast<MCSymbolRefExpr>(Value)->getSymbol());
102 break;
103
104 case MCExpr::Unary:
105 AddValueSymbols(cast<MCUnaryExpr>(Value)->getSubExpr());
106 break;
107 }
108
109 return Value;
110 }
111
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000112 /// @name MCStreamer Interface
113 /// @{
114
115 virtual void SwitchSection(const MCSection *Section);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000116 virtual void EmitLabel(MCSymbol *Symbol);
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000117 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag);
Daniel Dunbar821e3332009-08-31 08:09:28 +0000118 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value);
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000119 virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000120 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
Chris Lattner9eb158d2010-01-23 07:47:02 +0000121 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000122 unsigned ByteAlignment);
Chris Lattner99328ad2010-01-25 07:52:13 +0000123 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
124 assert(0 && "macho doesn't support this directive");
125 }
Chris Lattner9eb158d2010-01-23 07:47:02 +0000126 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size) {
127 assert(0 && "macho doesn't support this directive");
128 }
Daniel Dunbar8751b942009-08-28 05:48:22 +0000129 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000130 unsigned Size = 0, unsigned ByteAlignment = 0);
Chris Lattneraaec2052010-01-19 19:46:13 +0000131 virtual void EmitBytes(StringRef Data, unsigned AddrSpace);
Chris Lattneraaec2052010-01-19 19:46:13 +0000132 virtual void EmitValue(const MCExpr *Value, unsigned Size,unsigned AddrSpace);
Chris Lattner718fb592010-01-25 21:28:50 +0000133 virtual void EmitGPRel32Value(const MCExpr *Value) {
134 assert(0 && "macho doesn't support this directive");
135 }
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000136 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
137 unsigned ValueSize = 1,
138 unsigned MaxBytesToEmit = 0);
Kevin Enderby6e720482010-02-23 18:26:34 +0000139 virtual void EmitCodeAlignment(unsigned ByteAlignment,
140 unsigned MaxBytesToEmit = 0);
Daniel Dunbar821e3332009-08-31 08:09:28 +0000141 virtual void EmitValueToOffset(const MCExpr *Offset,
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000142 unsigned char Value = 0);
Chris Lattnera6594fc2010-01-25 18:58:59 +0000143
144 virtual void EmitFileDirective(StringRef Filename) {
145 errs() << "FIXME: MCMachoStreamer:EmitFileDirective not implemented\n";
146 }
147 virtual void EmitDwarfFileDirective(unsigned FileNo, StringRef Filename) {
148 errs() << "FIXME: MCMachoStreamer:EmitDwarfFileDirective not implemented\n";
149 }
150
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000151 virtual void EmitInstruction(const MCInst &Inst);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000152 virtual void Finish();
153
154 /// @}
155};
156
157} // end anonymous namespace.
158
159void MCMachOStreamer::SwitchSection(const MCSection *Section) {
160 assert(Section && "Cannot switch to a null section!");
Chris Lattnercda7f782009-08-22 19:35:08 +0000161
162 // If already in this section, then this is a noop.
163 if (Section == CurSection) return;
Daniel Dunbar0f5fa692009-08-28 05:48:54 +0000164
Chris Lattnercda7f782009-08-22 19:35:08 +0000165 CurSection = Section;
Daniel Dunbar0f5fa692009-08-28 05:48:54 +0000166 CurSectionData = &getSectionData(*Section);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000167}
168
169void MCMachOStreamer::EmitLabel(MCSymbol *Symbol) {
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000170 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000171
Daniel Dunbar5e835962009-08-26 02:48:04 +0000172 // FIXME: We should also use offsets into Fill fragments.
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000173 MCDataFragment *F = dyn_cast_or_null<MCDataFragment>(getCurrentFragment());
174 if (!F)
175 F = new MCDataFragment(CurSectionData);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000176
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000177 MCSymbolData &SD = getSymbolData(*Symbol);
178 assert(!SD.getFragment() && "Unexpected fragment on symbol data!");
179 SD.setFragment(F);
180 SD.setOffset(F->getContents().size());
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000181
182 // This causes the reference type and weak reference flags to be cleared.
183 SD.setFlags(SD.getFlags() & ~(SF_WeakReference | SF_ReferenceTypeMask));
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000184
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000185 Symbol->setSection(*CurSection);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000186}
187
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000188void MCMachOStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
Daniel Dunbar6009db42009-08-26 21:22:22 +0000189 switch (Flag) {
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000190 case MCAF_SubsectionsViaSymbols:
Daniel Dunbar6009db42009-08-26 21:22:22 +0000191 Assembler.setSubsectionsViaSymbols(true);
Daniel Dunbar8c3eaf42009-08-28 07:08:47 +0000192 return;
Daniel Dunbar6009db42009-08-26 21:22:22 +0000193 }
Daniel Dunbar8c3eaf42009-08-28 07:08:47 +0000194
195 assert(0 && "invalid assembler flag!");
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000196}
197
Daniel Dunbar821e3332009-08-31 08:09:28 +0000198void MCMachOStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000199 // Only absolute symbols can be redefined.
200 assert((Symbol->isUndefined() || Symbol->isAbsolute()) &&
201 "Cannot define a symbol twice!");
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000202
Daniel Dunbara4d86672009-10-16 01:58:23 +0000203 // FIXME: Lift context changes into super class.
204 // FIXME: Set associated section.
205 Symbol->setValue(Value);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000206}
207
208void MCMachOStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000209 MCSymbolAttr Attribute) {
Daniel Dunbar0c7761b2009-08-24 11:56:58 +0000210 // Indirect symbols are handled differently, to match how 'as' handles
211 // them. This makes writing matching .o files easier.
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000212 if (Attribute == MCSA_IndirectSymbol) {
Daniel Dunbarad7c3d52009-08-26 00:18:21 +0000213 // Note that we intentionally cannot use the symbol data here; this is
214 // important for matching the string table that 'as' generates.
Daniel Dunbar0c7761b2009-08-24 11:56:58 +0000215 IndirectSymbolData ISD;
216 ISD.Symbol = Symbol;
217 ISD.SectionData = CurSectionData;
218 Assembler.getIndirectSymbols().push_back(ISD);
219 return;
220 }
221
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000222 // Adding a symbol attribute always introduces the symbol, note that an
223 // important side effect of calling getSymbolData here is to register the
224 // symbol with the assembler.
225 MCSymbolData &SD = getSymbolData(*Symbol);
226
227 // The implementation of symbol attributes is designed to match 'as', but it
228 // leaves much to desired. It doesn't really make sense to arbitrarily add and
229 // remove flags, but 'as' allows this (in particular, see .desc).
230 //
231 // In the future it might be worth trying to make these operations more well
232 // defined.
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000233 switch (Attribute) {
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000234 case MCSA_Invalid:
Chris Lattnered0ab152010-01-25 18:30:45 +0000235 case MCSA_ELF_TypeFunction:
236 case MCSA_ELF_TypeIndFunction:
237 case MCSA_ELF_TypeObject:
238 case MCSA_ELF_TypeTLS:
239 case MCSA_ELF_TypeCommon:
240 case MCSA_ELF_TypeNoType:
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000241 case MCSA_IndirectSymbol:
242 case MCSA_Hidden:
243 case MCSA_Internal:
244 case MCSA_Protected:
245 case MCSA_Weak:
246 case MCSA_Local:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000247 assert(0 && "Invalid symbol attribute for Mach-O!");
248 break;
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000249
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000250 case MCSA_Global:
Daniel Dunbar8f4d1462009-08-28 07:08:35 +0000251 SD.setExternal(true);
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000252 break;
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000253
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000254 case MCSA_LazyReference:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000255 // FIXME: This requires -dynamic.
256 SD.setFlags(SD.getFlags() | SF_NoDeadStrip);
257 if (Symbol->isUndefined())
258 SD.setFlags(SD.getFlags() | SF_ReferenceTypeUndefinedLazy);
259 break;
260
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000261 // Since .reference sets the no dead strip bit, it is equivalent to
262 // .no_dead_strip in practice.
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000263 case MCSA_Reference:
264 case MCSA_NoDeadStrip:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000265 SD.setFlags(SD.getFlags() | SF_NoDeadStrip);
266 break;
267
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000268 case MCSA_PrivateExtern:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000269 SD.setExternal(true);
270 SD.setPrivateExtern(true);
271 break;
272
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000273 case MCSA_WeakReference:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000274 // FIXME: This requires -dynamic.
275 if (Symbol->isUndefined())
276 SD.setFlags(SD.getFlags() | SF_WeakReference);
277 break;
278
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000279 case MCSA_WeakDefinition:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000280 // FIXME: 'as' enforces that this is defined and global. The manual claims
281 // it has to be in a coalesced section, but this isn't enforced.
282 SD.setFlags(SD.getFlags() | SF_WeakDefinition);
283 break;
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000284 }
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000285}
286
287void MCMachOStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000288 // Encode the 'desc' value into the lowest implementation defined bits.
289 assert(DescValue == (DescValue & SF_DescFlagsMask) &&
290 "Invalid .desc value!");
291 getSymbolData(*Symbol).setFlags(DescValue & SF_DescFlagsMask);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000292}
293
Chris Lattner9eb158d2010-01-23 07:47:02 +0000294void MCMachOStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000295 unsigned ByteAlignment) {
Daniel Dunbar8f4d1462009-08-28 07:08:35 +0000296 // FIXME: Darwin 'as' does appear to allow redef of a .comm by itself.
297 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
298
299 MCSymbolData &SD = getSymbolData(*Symbol);
300 SD.setExternal(true);
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000301 SD.setCommon(Size, ByteAlignment);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000302}
303
Daniel Dunbar8751b942009-08-28 05:48:22 +0000304void MCMachOStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000305 unsigned Size, unsigned ByteAlignment) {
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000306 MCSectionData &SectData = getSectionData(*Section);
307
308 // The symbol may not be present, which only creates the section.
309 if (!Symbol)
310 return;
311
312 // FIXME: Assert that this section has the zerofill type.
313
314 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
315
316 MCSymbolData &SD = getSymbolData(*Symbol);
317
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000318 MCFragment *F = new MCZeroFillFragment(Size, ByteAlignment, &SectData);
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000319 SD.setFragment(F);
320
321 Symbol->setSection(*Section);
322
323 // Update the maximum alignment on the zero fill section if necessary.
324 if (ByteAlignment > SectData.getAlignment())
325 SectData.setAlignment(ByteAlignment);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000326}
327
Chris Lattneraaec2052010-01-19 19:46:13 +0000328void MCMachOStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000329 MCDataFragment *DF = dyn_cast_or_null<MCDataFragment>(getCurrentFragment());
330 if (!DF)
331 DF = new MCDataFragment(CurSectionData);
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000332 DF->getContents().append(Data.begin(), Data.end());
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000333}
334
Chris Lattneraaec2052010-01-19 19:46:13 +0000335void MCMachOStreamer::EmitValue(const MCExpr *Value, unsigned Size,
336 unsigned AddrSpace) {
Daniel Dunbar45f48742010-02-13 09:28:22 +0000337 MCDataFragment *DF = dyn_cast_or_null<MCDataFragment>(getCurrentFragment());
338 if (!DF)
339 DF = new MCDataFragment(CurSectionData);
340
341 // Avoid fixups when possible.
342 int64_t AbsValue;
Daniel Dunbar40ebe2472010-02-22 22:08:57 +0000343 if (AddValueSymbols(Value)->EvaluateAsAbsolute(AbsValue)) {
Daniel Dunbar45f48742010-02-13 09:28:22 +0000344 // FIXME: Endianness assumption.
345 for (unsigned i = 0; i != Size; ++i)
346 DF->getContents().push_back(uint8_t(AbsValue >> (i * 8)));
347 } else {
348 DF->getFixups().push_back(MCAsmFixup(DF->getContents().size(),
Daniel Dunbar2be2fd02010-02-13 09:28:54 +0000349 *AddValueSymbols(Value),
350 MCFixup::getKindForSize(Size)));
Daniel Dunbar45f48742010-02-13 09:28:22 +0000351 DF->getContents().resize(DF->getContents().size() + Size, 0);
352 }
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000353}
354
355void MCMachOStreamer::EmitValueToAlignment(unsigned ByteAlignment,
356 int64_t Value, unsigned ValueSize,
357 unsigned MaxBytesToEmit) {
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000358 if (MaxBytesToEmit == 0)
359 MaxBytesToEmit = ByteAlignment;
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000360 new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit,
Kevin Enderby6e720482010-02-23 18:26:34 +0000361 false /* EmitNops */, CurSectionData);
362
363 // Update the maximum alignment on the current section if necessary.
364 if (ByteAlignment > CurSectionData->getAlignment())
365 CurSectionData->setAlignment(ByteAlignment);
366}
367
368void MCMachOStreamer::EmitCodeAlignment(unsigned ByteAlignment,
369 unsigned MaxBytesToEmit) {
370 if (MaxBytesToEmit == 0)
371 MaxBytesToEmit = ByteAlignment;
372 // FIXME the 0x90 is the default x86 1 byte nop opcode.
373 new MCAlignFragment(ByteAlignment, 0x90, 1, MaxBytesToEmit,
374 true /* EmitNops */, CurSectionData);
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000375
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000376 // Update the maximum alignment on the current section if necessary.
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000377 if (ByteAlignment > CurSectionData->getAlignment())
378 CurSectionData->setAlignment(ByteAlignment);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000379}
380
Daniel Dunbar821e3332009-08-31 08:09:28 +0000381void MCMachOStreamer::EmitValueToOffset(const MCExpr *Offset,
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000382 unsigned char Value) {
Daniel Dunbar1253a6f2009-10-16 01:58:03 +0000383 new MCOrgFragment(*Offset, Value, CurSectionData);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000384}
385
386void MCMachOStreamer::EmitInstruction(const MCInst &Inst) {
Daniel Dunbar4fac7492009-08-27 08:17:51 +0000387 // Scan for values.
388 for (unsigned i = 0; i != Inst.getNumOperands(); ++i)
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000389 if (Inst.getOperand(i).isExpr())
390 AddValueSymbols(Inst.getOperand(i).getExpr());
Daniel Dunbar4fac7492009-08-27 08:17:51 +0000391
392 if (!Emitter)
393 llvm_unreachable("no code emitter available!");
394
Daniel Dunbare1ec6172010-02-02 21:44:01 +0000395 CurSectionData->setHasInstructions(true);
396
Daniel Dunbar73c55742010-02-09 22:59:55 +0000397 SmallVector<MCFixup, 4> Fixups;
Daniel Dunbar4fac7492009-08-27 08:17:51 +0000398 SmallString<256> Code;
399 raw_svector_ostream VecOS(Code);
Daniel Dunbar73c55742010-02-09 22:59:55 +0000400 Emitter->EncodeInstruction(Inst, VecOS, Fixups);
Daniel Dunbarf6346762010-02-13 09:29:02 +0000401 VecOS.flush();
402
403 // Add the fixups and data.
404 MCDataFragment *DF = dyn_cast_or_null<MCDataFragment>(getCurrentFragment());
405 if (!DF)
406 DF = new MCDataFragment(CurSectionData);
407 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
408 MCFixup &F = Fixups[i];
409 DF->getFixups().push_back(MCAsmFixup(DF->getContents().size()+F.getOffset(),
410 *F.getValue(),
411 F.getKind()));
412 }
413 DF->getContents().append(Code.begin(), Code.end());
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000414}
415
416void MCMachOStreamer::Finish() {
417 Assembler.Finish();
418}
419
Daniel Dunbar4fac7492009-08-27 08:17:51 +0000420MCStreamer *llvm::createMachOStreamer(MCContext &Context, raw_ostream &OS,
421 MCCodeEmitter *CE) {
422 return new MCMachOStreamer(Context, OS, CE);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000423}