blob: 51a2d45ed71bbe3c9b75acda4ec779a693cb976a [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 Dunbarf70f4772010-03-22 20:35:46 +000060 /// Get a data fragment to write into, creating a new one if the current
61 /// fragment is not a data fragment.
62 MCDataFragment *getOrCreateDataFragment() const {
63 MCDataFragment *F = dyn_cast_or_null<MCDataFragment>(getCurrentFragment());
64 if (!F)
65 F = new MCDataFragment(CurSectionData);
66 return F;
67 }
68
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000069public:
Daniel Dunbar1f3e4452010-03-11 01:34:27 +000070 MCMachOStreamer(MCContext &Context, TargetAsmBackend &TAB,
71 raw_ostream &_OS, MCCodeEmitter *_Emitter)
Daniel Dunbarcf871e52010-03-19 10:43:18 +000072 : MCStreamer(Context), Assembler(Context, TAB, *_Emitter, _OS),
Daniel Dunbar4fac7492009-08-27 08:17:51 +000073 CurSectionData(0) {}
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000074 ~MCMachOStreamer() {}
75
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +000076 const MCExpr *AddValueSymbols(const MCExpr *Value) {
77 switch (Value->getKind()) {
Chris Lattner5d917a82010-02-08 19:41:07 +000078 case MCExpr::Target: assert(0 && "Can't handle target exprs yet!");
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +000079 case MCExpr::Constant:
80 break;
81
82 case MCExpr::Binary: {
83 const MCBinaryExpr *BE = cast<MCBinaryExpr>(Value);
84 AddValueSymbols(BE->getLHS());
85 AddValueSymbols(BE->getRHS());
86 break;
87 }
88
89 case MCExpr::SymbolRef:
Daniel Dunbar46836a72010-03-10 20:58:29 +000090 Assembler.getOrCreateSymbolData(
91 cast<MCSymbolRefExpr>(Value)->getSymbol());
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +000092 break;
93
94 case MCExpr::Unary:
95 AddValueSymbols(cast<MCUnaryExpr>(Value)->getSubExpr());
96 break;
97 }
98
99 return Value;
100 }
101
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000102 /// @name MCStreamer Interface
103 /// @{
104
105 virtual void SwitchSection(const MCSection *Section);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000106 virtual void EmitLabel(MCSymbol *Symbol);
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000107 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag);
Daniel Dunbar821e3332009-08-31 08:09:28 +0000108 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value);
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000109 virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000110 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
Chris Lattner9eb158d2010-01-23 07:47:02 +0000111 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000112 unsigned ByteAlignment);
Chris Lattner99328ad2010-01-25 07:52:13 +0000113 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
114 assert(0 && "macho doesn't support this directive");
115 }
Chris Lattner9eb158d2010-01-23 07:47:02 +0000116 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size) {
117 assert(0 && "macho doesn't support this directive");
118 }
Daniel Dunbar8751b942009-08-28 05:48:22 +0000119 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000120 unsigned Size = 0, unsigned ByteAlignment = 0);
Chris Lattneraaec2052010-01-19 19:46:13 +0000121 virtual void EmitBytes(StringRef Data, unsigned AddrSpace);
Chris Lattneraaec2052010-01-19 19:46:13 +0000122 virtual void EmitValue(const MCExpr *Value, unsigned Size,unsigned AddrSpace);
Chris Lattner718fb592010-01-25 21:28:50 +0000123 virtual void EmitGPRel32Value(const MCExpr *Value) {
124 assert(0 && "macho doesn't support this directive");
125 }
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000126 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
127 unsigned ValueSize = 1,
128 unsigned MaxBytesToEmit = 0);
Kevin Enderby6e720482010-02-23 18:26:34 +0000129 virtual void EmitCodeAlignment(unsigned ByteAlignment,
130 unsigned MaxBytesToEmit = 0);
Daniel Dunbar821e3332009-08-31 08:09:28 +0000131 virtual void EmitValueToOffset(const MCExpr *Offset,
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000132 unsigned char Value = 0);
Chris Lattnera6594fc2010-01-25 18:58:59 +0000133
134 virtual void EmitFileDirective(StringRef Filename) {
135 errs() << "FIXME: MCMachoStreamer:EmitFileDirective not implemented\n";
136 }
137 virtual void EmitDwarfFileDirective(unsigned FileNo, StringRef Filename) {
138 errs() << "FIXME: MCMachoStreamer:EmitDwarfFileDirective not implemented\n";
139 }
140
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000141 virtual void EmitInstruction(const MCInst &Inst);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000142 virtual void Finish();
143
144 /// @}
145};
146
147} // end anonymous namespace.
148
149void MCMachOStreamer::SwitchSection(const MCSection *Section) {
150 assert(Section && "Cannot switch to a null section!");
Chris Lattnercda7f782009-08-22 19:35:08 +0000151
152 // If already in this section, then this is a noop.
153 if (Section == CurSection) return;
Daniel Dunbar0f5fa692009-08-28 05:48:54 +0000154
Chris Lattnercda7f782009-08-22 19:35:08 +0000155 CurSection = Section;
Daniel Dunbar46836a72010-03-10 20:58:29 +0000156 CurSectionData = &Assembler.getOrCreateSectionData(*Section);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000157}
158
159void MCMachOStreamer::EmitLabel(MCSymbol *Symbol) {
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000160 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000161
Daniel Dunbarf70f4772010-03-22 20:35:46 +0000162 // FIXME: This is wasteful, we don't necessarily need to create a data
163 // fragment. Instead, we should mark the symbol as pointing into the data
164 // fragment if it exists, otherwise we should just queue the label and set its
165 // fragment pointer when we emit the next fragment.
166 MCDataFragment *F = getOrCreateDataFragment();
Daniel Dunbar46836a72010-03-10 20:58:29 +0000167 MCSymbolData &SD = Assembler.getOrCreateSymbolData(*Symbol);
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000168 assert(!SD.getFragment() && "Unexpected fragment on symbol data!");
169 SD.setFragment(F);
170 SD.setOffset(F->getContents().size());
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000171
172 // This causes the reference type and weak reference flags to be cleared.
173 SD.setFlags(SD.getFlags() & ~(SF_WeakReference | SF_ReferenceTypeMask));
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000174
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000175 Symbol->setSection(*CurSection);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000176}
177
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000178void MCMachOStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
Daniel Dunbar6009db42009-08-26 21:22:22 +0000179 switch (Flag) {
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000180 case MCAF_SubsectionsViaSymbols:
Daniel Dunbar6009db42009-08-26 21:22:22 +0000181 Assembler.setSubsectionsViaSymbols(true);
Daniel Dunbar8c3eaf42009-08-28 07:08:47 +0000182 return;
Daniel Dunbar6009db42009-08-26 21:22:22 +0000183 }
Daniel Dunbar8c3eaf42009-08-28 07:08:47 +0000184
185 assert(0 && "invalid assembler flag!");
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000186}
187
Daniel Dunbar821e3332009-08-31 08:09:28 +0000188void MCMachOStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000189 // Only absolute symbols can be redefined.
190 assert((Symbol->isUndefined() || Symbol->isAbsolute()) &&
191 "Cannot define a symbol twice!");
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000192
Daniel Dunbara4d86672009-10-16 01:58:23 +0000193 // FIXME: Lift context changes into super class.
194 // FIXME: Set associated section.
Daniel Dunbard3da3622010-03-14 03:10:40 +0000195 Symbol->setValue(AddValueSymbols(Value));
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000196}
197
198void MCMachOStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000199 MCSymbolAttr Attribute) {
Daniel Dunbar0c7761b2009-08-24 11:56:58 +0000200 // Indirect symbols are handled differently, to match how 'as' handles
201 // them. This makes writing matching .o files easier.
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000202 if (Attribute == MCSA_IndirectSymbol) {
Daniel Dunbarad7c3d52009-08-26 00:18:21 +0000203 // Note that we intentionally cannot use the symbol data here; this is
204 // important for matching the string table that 'as' generates.
Daniel Dunbar0c7761b2009-08-24 11:56:58 +0000205 IndirectSymbolData ISD;
206 ISD.Symbol = Symbol;
207 ISD.SectionData = CurSectionData;
208 Assembler.getIndirectSymbols().push_back(ISD);
209 return;
210 }
211
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000212 // Adding a symbol attribute always introduces the symbol, note that an
Daniel Dunbar46836a72010-03-10 20:58:29 +0000213 // important side effect of calling getOrCreateSymbolData here is to register
214 // the symbol with the assembler.
215 MCSymbolData &SD = Assembler.getOrCreateSymbolData(*Symbol);
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000216
217 // The implementation of symbol attributes is designed to match 'as', but it
218 // leaves much to desired. It doesn't really make sense to arbitrarily add and
219 // remove flags, but 'as' allows this (in particular, see .desc).
220 //
221 // In the future it might be worth trying to make these operations more well
222 // defined.
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000223 switch (Attribute) {
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000224 case MCSA_Invalid:
Chris Lattnered0ab152010-01-25 18:30:45 +0000225 case MCSA_ELF_TypeFunction:
226 case MCSA_ELF_TypeIndFunction:
227 case MCSA_ELF_TypeObject:
228 case MCSA_ELF_TypeTLS:
229 case MCSA_ELF_TypeCommon:
230 case MCSA_ELF_TypeNoType:
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000231 case MCSA_IndirectSymbol:
232 case MCSA_Hidden:
233 case MCSA_Internal:
234 case MCSA_Protected:
235 case MCSA_Weak:
236 case MCSA_Local:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000237 assert(0 && "Invalid symbol attribute for Mach-O!");
238 break;
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000239
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000240 case MCSA_Global:
Daniel Dunbar8f4d1462009-08-28 07:08:35 +0000241 SD.setExternal(true);
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000242 break;
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000243
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000244 case MCSA_LazyReference:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000245 // FIXME: This requires -dynamic.
246 SD.setFlags(SD.getFlags() | SF_NoDeadStrip);
247 if (Symbol->isUndefined())
248 SD.setFlags(SD.getFlags() | SF_ReferenceTypeUndefinedLazy);
249 break;
250
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000251 // Since .reference sets the no dead strip bit, it is equivalent to
252 // .no_dead_strip in practice.
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000253 case MCSA_Reference:
254 case MCSA_NoDeadStrip:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000255 SD.setFlags(SD.getFlags() | SF_NoDeadStrip);
256 break;
257
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000258 case MCSA_PrivateExtern:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000259 SD.setExternal(true);
260 SD.setPrivateExtern(true);
261 break;
262
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000263 case MCSA_WeakReference:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000264 // FIXME: This requires -dynamic.
265 if (Symbol->isUndefined())
266 SD.setFlags(SD.getFlags() | SF_WeakReference);
267 break;
268
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000269 case MCSA_WeakDefinition:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000270 // FIXME: 'as' enforces that this is defined and global. The manual claims
271 // it has to be in a coalesced section, but this isn't enforced.
272 SD.setFlags(SD.getFlags() | SF_WeakDefinition);
273 break;
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000274 }
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000275}
276
277void MCMachOStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000278 // Encode the 'desc' value into the lowest implementation defined bits.
279 assert(DescValue == (DescValue & SF_DescFlagsMask) &&
280 "Invalid .desc value!");
Daniel Dunbar46836a72010-03-10 20:58:29 +0000281 Assembler.getOrCreateSymbolData(*Symbol).setFlags(DescValue&SF_DescFlagsMask);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000282}
283
Chris Lattner9eb158d2010-01-23 07:47:02 +0000284void MCMachOStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000285 unsigned ByteAlignment) {
Daniel Dunbar8f4d1462009-08-28 07:08:35 +0000286 // FIXME: Darwin 'as' does appear to allow redef of a .comm by itself.
287 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
288
Daniel Dunbar46836a72010-03-10 20:58:29 +0000289 MCSymbolData &SD = Assembler.getOrCreateSymbolData(*Symbol);
Daniel Dunbar8f4d1462009-08-28 07:08:35 +0000290 SD.setExternal(true);
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000291 SD.setCommon(Size, ByteAlignment);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000292}
293
Daniel Dunbar8751b942009-08-28 05:48:22 +0000294void MCMachOStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000295 unsigned Size, unsigned ByteAlignment) {
Daniel Dunbar46836a72010-03-10 20:58:29 +0000296 MCSectionData &SectData = Assembler.getOrCreateSectionData(*Section);
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000297
298 // The symbol may not be present, which only creates the section.
299 if (!Symbol)
300 return;
301
302 // FIXME: Assert that this section has the zerofill type.
303
304 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
305
Daniel Dunbar46836a72010-03-10 20:58:29 +0000306 MCSymbolData &SD = Assembler.getOrCreateSymbolData(*Symbol);
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000307
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000308 MCFragment *F = new MCZeroFillFragment(Size, ByteAlignment, &SectData);
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000309 SD.setFragment(F);
310
311 Symbol->setSection(*Section);
312
313 // Update the maximum alignment on the zero fill section if necessary.
314 if (ByteAlignment > SectData.getAlignment())
315 SectData.setAlignment(ByteAlignment);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000316}
317
Chris Lattneraaec2052010-01-19 19:46:13 +0000318void MCMachOStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {
Daniel Dunbarf70f4772010-03-22 20:35:46 +0000319 getOrCreateDataFragment()->getContents().append(Data.begin(), Data.end());
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000320}
321
Chris Lattneraaec2052010-01-19 19:46:13 +0000322void MCMachOStreamer::EmitValue(const MCExpr *Value, unsigned Size,
323 unsigned AddrSpace) {
Daniel Dunbarf70f4772010-03-22 20:35:46 +0000324 MCDataFragment *DF = getOrCreateDataFragment();
Daniel Dunbar45f48742010-02-13 09:28:22 +0000325
326 // Avoid fixups when possible.
327 int64_t AbsValue;
Daniel Dunbar40ebe2472010-02-22 22:08:57 +0000328 if (AddValueSymbols(Value)->EvaluateAsAbsolute(AbsValue)) {
Daniel Dunbar45f48742010-02-13 09:28:22 +0000329 // FIXME: Endianness assumption.
330 for (unsigned i = 0; i != Size; ++i)
331 DF->getContents().push_back(uint8_t(AbsValue >> (i * 8)));
332 } else {
Daniel Dunbar8315a352010-03-12 21:00:38 +0000333 DF->addFixup(MCAsmFixup(DF->getContents().size(), *AddValueSymbols(Value),
334 MCFixup::getKindForSize(Size)));
Daniel Dunbar45f48742010-02-13 09:28:22 +0000335 DF->getContents().resize(DF->getContents().size() + Size, 0);
336 }
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000337}
338
339void MCMachOStreamer::EmitValueToAlignment(unsigned ByteAlignment,
340 int64_t Value, unsigned ValueSize,
341 unsigned MaxBytesToEmit) {
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000342 if (MaxBytesToEmit == 0)
343 MaxBytesToEmit = ByteAlignment;
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000344 new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit,
Kevin Enderby6e720482010-02-23 18:26:34 +0000345 false /* EmitNops */, CurSectionData);
346
347 // Update the maximum alignment on the current section if necessary.
348 if (ByteAlignment > CurSectionData->getAlignment())
349 CurSectionData->setAlignment(ByteAlignment);
350}
351
352void MCMachOStreamer::EmitCodeAlignment(unsigned ByteAlignment,
353 unsigned MaxBytesToEmit) {
354 if (MaxBytesToEmit == 0)
355 MaxBytesToEmit = ByteAlignment;
Daniel Dunbarf70f4772010-03-22 20:35:46 +0000356 // FIXME: The 0x90 is the default x86 1 byte nop opcode.
Kevin Enderby6e720482010-02-23 18:26:34 +0000357 new MCAlignFragment(ByteAlignment, 0x90, 1, MaxBytesToEmit,
358 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
387 // Add the fixups and data.
Daniel Dunbarf70f4772010-03-22 20:35:46 +0000388 MCDataFragment *DF = getOrCreateDataFragment();
Daniel Dunbarf6346762010-02-13 09:29:02 +0000389 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
390 MCFixup &F = Fixups[i];
Daniel Dunbar8315a352010-03-12 21:00:38 +0000391 DF->addFixup(MCAsmFixup(DF->getContents().size()+F.getOffset(),
392 *F.getValue(), F.getKind()));
Daniel Dunbarf6346762010-02-13 09:29:02 +0000393 }
394 DF->getContents().append(Code.begin(), Code.end());
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000395}
396
397void MCMachOStreamer::Finish() {
398 Assembler.Finish();
399}
400
Daniel Dunbar1f3e4452010-03-11 01:34:27 +0000401MCStreamer *llvm::createMachOStreamer(MCContext &Context, TargetAsmBackend &TAB,
402 raw_ostream &OS, MCCodeEmitter *CE) {
403 return new MCMachOStreamer(Context, TAB, OS, CE);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000404}