blob: 2d833ba351fe2e39f56dc3b628f719a6d571d1e7 [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 Dunbarf3d2ef02009-08-22 10:13:24 +000050
51private:
52 MCFragment *getCurrentFragment() const {
53 assert(CurSectionData && "No current section!");
54
55 if (!CurSectionData->empty())
56 return &CurSectionData->getFragmentList().back();
57
58 return 0;
59 }
60
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000061public:
Daniel Dunbar1f3e4452010-03-11 01:34:27 +000062 MCMachOStreamer(MCContext &Context, TargetAsmBackend &TAB,
63 raw_ostream &_OS, MCCodeEmitter *_Emitter)
64 : MCStreamer(Context), Assembler(Context, TAB, _OS), Emitter(_Emitter),
Daniel Dunbar4fac7492009-08-27 08:17:51 +000065 CurSectionData(0) {}
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000066 ~MCMachOStreamer() {}
67
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +000068 const MCExpr *AddValueSymbols(const MCExpr *Value) {
69 switch (Value->getKind()) {
Chris Lattner5d917a82010-02-08 19:41:07 +000070 case MCExpr::Target: assert(0 && "Can't handle target exprs yet!");
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +000071 case MCExpr::Constant:
72 break;
73
74 case MCExpr::Binary: {
75 const MCBinaryExpr *BE = cast<MCBinaryExpr>(Value);
76 AddValueSymbols(BE->getLHS());
77 AddValueSymbols(BE->getRHS());
78 break;
79 }
80
81 case MCExpr::SymbolRef:
Daniel Dunbar46836a72010-03-10 20:58:29 +000082 Assembler.getOrCreateSymbolData(
83 cast<MCSymbolRefExpr>(Value)->getSymbol());
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +000084 break;
85
86 case MCExpr::Unary:
87 AddValueSymbols(cast<MCUnaryExpr>(Value)->getSubExpr());
88 break;
89 }
90
91 return Value;
92 }
93
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000094 /// @name MCStreamer Interface
95 /// @{
96
97 virtual void SwitchSection(const MCSection *Section);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000098 virtual void EmitLabel(MCSymbol *Symbol);
Chris Lattnera5ad93a2010-01-23 06:39:22 +000099 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag);
Daniel Dunbar821e3332009-08-31 08:09:28 +0000100 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value);
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000101 virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000102 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
Chris Lattner9eb158d2010-01-23 07:47:02 +0000103 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000104 unsigned ByteAlignment);
Chris Lattner99328ad2010-01-25 07:52:13 +0000105 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
106 assert(0 && "macho doesn't support this directive");
107 }
Chris Lattner9eb158d2010-01-23 07:47:02 +0000108 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size) {
109 assert(0 && "macho doesn't support this directive");
110 }
Daniel Dunbar8751b942009-08-28 05:48:22 +0000111 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000112 unsigned Size = 0, unsigned ByteAlignment = 0);
Chris Lattneraaec2052010-01-19 19:46:13 +0000113 virtual void EmitBytes(StringRef Data, unsigned AddrSpace);
Chris Lattneraaec2052010-01-19 19:46:13 +0000114 virtual void EmitValue(const MCExpr *Value, unsigned Size,unsigned AddrSpace);
Chris Lattner718fb592010-01-25 21:28:50 +0000115 virtual void EmitGPRel32Value(const MCExpr *Value) {
116 assert(0 && "macho doesn't support this directive");
117 }
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000118 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
119 unsigned ValueSize = 1,
120 unsigned MaxBytesToEmit = 0);
Kevin Enderby6e720482010-02-23 18:26:34 +0000121 virtual void EmitCodeAlignment(unsigned ByteAlignment,
122 unsigned MaxBytesToEmit = 0);
Daniel Dunbar821e3332009-08-31 08:09:28 +0000123 virtual void EmitValueToOffset(const MCExpr *Offset,
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000124 unsigned char Value = 0);
Chris Lattnera6594fc2010-01-25 18:58:59 +0000125
126 virtual void EmitFileDirective(StringRef Filename) {
127 errs() << "FIXME: MCMachoStreamer:EmitFileDirective not implemented\n";
128 }
129 virtual void EmitDwarfFileDirective(unsigned FileNo, StringRef Filename) {
130 errs() << "FIXME: MCMachoStreamer:EmitDwarfFileDirective not implemented\n";
131 }
132
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000133 virtual void EmitInstruction(const MCInst &Inst);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000134 virtual void Finish();
135
136 /// @}
137};
138
139} // end anonymous namespace.
140
141void MCMachOStreamer::SwitchSection(const MCSection *Section) {
142 assert(Section && "Cannot switch to a null section!");
Chris Lattnercda7f782009-08-22 19:35:08 +0000143
144 // If already in this section, then this is a noop.
145 if (Section == CurSection) return;
Daniel Dunbar0f5fa692009-08-28 05:48:54 +0000146
Chris Lattnercda7f782009-08-22 19:35:08 +0000147 CurSection = Section;
Daniel Dunbar46836a72010-03-10 20:58:29 +0000148 CurSectionData = &Assembler.getOrCreateSectionData(*Section);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000149}
150
151void MCMachOStreamer::EmitLabel(MCSymbol *Symbol) {
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000152 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000153
Daniel Dunbar5e835962009-08-26 02:48:04 +0000154 // FIXME: We should also use offsets into Fill fragments.
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000155 MCDataFragment *F = dyn_cast_or_null<MCDataFragment>(getCurrentFragment());
156 if (!F)
157 F = new MCDataFragment(CurSectionData);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000158
Daniel Dunbar46836a72010-03-10 20:58:29 +0000159 MCSymbolData &SD = Assembler.getOrCreateSymbolData(*Symbol);
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000160 assert(!SD.getFragment() && "Unexpected fragment on symbol data!");
161 SD.setFragment(F);
162 SD.setOffset(F->getContents().size());
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000163
164 // This causes the reference type and weak reference flags to be cleared.
165 SD.setFlags(SD.getFlags() & ~(SF_WeakReference | SF_ReferenceTypeMask));
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000166
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000167 Symbol->setSection(*CurSection);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000168}
169
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000170void MCMachOStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
Daniel Dunbar6009db42009-08-26 21:22:22 +0000171 switch (Flag) {
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000172 case MCAF_SubsectionsViaSymbols:
Daniel Dunbar6009db42009-08-26 21:22:22 +0000173 Assembler.setSubsectionsViaSymbols(true);
Daniel Dunbar8c3eaf42009-08-28 07:08:47 +0000174 return;
Daniel Dunbar6009db42009-08-26 21:22:22 +0000175 }
Daniel Dunbar8c3eaf42009-08-28 07:08:47 +0000176
177 assert(0 && "invalid assembler flag!");
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000178}
179
Daniel Dunbar821e3332009-08-31 08:09:28 +0000180void MCMachOStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000181 // Only absolute symbols can be redefined.
182 assert((Symbol->isUndefined() || Symbol->isAbsolute()) &&
183 "Cannot define a symbol twice!");
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000184
Daniel Dunbara4d86672009-10-16 01:58:23 +0000185 // FIXME: Lift context changes into super class.
186 // FIXME: Set associated section.
187 Symbol->setValue(Value);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000188}
189
190void MCMachOStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000191 MCSymbolAttr Attribute) {
Daniel Dunbar0c7761b2009-08-24 11:56:58 +0000192 // Indirect symbols are handled differently, to match how 'as' handles
193 // them. This makes writing matching .o files easier.
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000194 if (Attribute == MCSA_IndirectSymbol) {
Daniel Dunbarad7c3d52009-08-26 00:18:21 +0000195 // Note that we intentionally cannot use the symbol data here; this is
196 // important for matching the string table that 'as' generates.
Daniel Dunbar0c7761b2009-08-24 11:56:58 +0000197 IndirectSymbolData ISD;
198 ISD.Symbol = Symbol;
199 ISD.SectionData = CurSectionData;
200 Assembler.getIndirectSymbols().push_back(ISD);
201 return;
202 }
203
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000204 // Adding a symbol attribute always introduces the symbol, note that an
Daniel Dunbar46836a72010-03-10 20:58:29 +0000205 // important side effect of calling getOrCreateSymbolData here is to register
206 // the symbol with the assembler.
207 MCSymbolData &SD = Assembler.getOrCreateSymbolData(*Symbol);
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000208
209 // The implementation of symbol attributes is designed to match 'as', but it
210 // leaves much to desired. It doesn't really make sense to arbitrarily add and
211 // remove flags, but 'as' allows this (in particular, see .desc).
212 //
213 // In the future it might be worth trying to make these operations more well
214 // defined.
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000215 switch (Attribute) {
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000216 case MCSA_Invalid:
Chris Lattnered0ab152010-01-25 18:30:45 +0000217 case MCSA_ELF_TypeFunction:
218 case MCSA_ELF_TypeIndFunction:
219 case MCSA_ELF_TypeObject:
220 case MCSA_ELF_TypeTLS:
221 case MCSA_ELF_TypeCommon:
222 case MCSA_ELF_TypeNoType:
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000223 case MCSA_IndirectSymbol:
224 case MCSA_Hidden:
225 case MCSA_Internal:
226 case MCSA_Protected:
227 case MCSA_Weak:
228 case MCSA_Local:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000229 assert(0 && "Invalid symbol attribute for Mach-O!");
230 break;
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000231
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000232 case MCSA_Global:
Daniel Dunbar8f4d1462009-08-28 07:08:35 +0000233 SD.setExternal(true);
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000234 break;
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000235
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000236 case MCSA_LazyReference:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000237 // FIXME: This requires -dynamic.
238 SD.setFlags(SD.getFlags() | SF_NoDeadStrip);
239 if (Symbol->isUndefined())
240 SD.setFlags(SD.getFlags() | SF_ReferenceTypeUndefinedLazy);
241 break;
242
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000243 // Since .reference sets the no dead strip bit, it is equivalent to
244 // .no_dead_strip in practice.
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000245 case MCSA_Reference:
246 case MCSA_NoDeadStrip:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000247 SD.setFlags(SD.getFlags() | SF_NoDeadStrip);
248 break;
249
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000250 case MCSA_PrivateExtern:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000251 SD.setExternal(true);
252 SD.setPrivateExtern(true);
253 break;
254
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000255 case MCSA_WeakReference:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000256 // FIXME: This requires -dynamic.
257 if (Symbol->isUndefined())
258 SD.setFlags(SD.getFlags() | SF_WeakReference);
259 break;
260
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000261 case MCSA_WeakDefinition:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000262 // FIXME: 'as' enforces that this is defined and global. The manual claims
263 // it has to be in a coalesced section, but this isn't enforced.
264 SD.setFlags(SD.getFlags() | SF_WeakDefinition);
265 break;
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000266 }
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000267}
268
269void MCMachOStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000270 // Encode the 'desc' value into the lowest implementation defined bits.
271 assert(DescValue == (DescValue & SF_DescFlagsMask) &&
272 "Invalid .desc value!");
Daniel Dunbar46836a72010-03-10 20:58:29 +0000273 Assembler.getOrCreateSymbolData(*Symbol).setFlags(DescValue&SF_DescFlagsMask);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000274}
275
Chris Lattner9eb158d2010-01-23 07:47:02 +0000276void MCMachOStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000277 unsigned ByteAlignment) {
Daniel Dunbar8f4d1462009-08-28 07:08:35 +0000278 // FIXME: Darwin 'as' does appear to allow redef of a .comm by itself.
279 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
280
Daniel Dunbar46836a72010-03-10 20:58:29 +0000281 MCSymbolData &SD = Assembler.getOrCreateSymbolData(*Symbol);
Daniel Dunbar8f4d1462009-08-28 07:08:35 +0000282 SD.setExternal(true);
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000283 SD.setCommon(Size, ByteAlignment);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000284}
285
Daniel Dunbar8751b942009-08-28 05:48:22 +0000286void MCMachOStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000287 unsigned Size, unsigned ByteAlignment) {
Daniel Dunbar46836a72010-03-10 20:58:29 +0000288 MCSectionData &SectData = Assembler.getOrCreateSectionData(*Section);
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000289
290 // The symbol may not be present, which only creates the section.
291 if (!Symbol)
292 return;
293
294 // FIXME: Assert that this section has the zerofill type.
295
296 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
297
Daniel Dunbar46836a72010-03-10 20:58:29 +0000298 MCSymbolData &SD = Assembler.getOrCreateSymbolData(*Symbol);
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000299
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000300 MCFragment *F = new MCZeroFillFragment(Size, ByteAlignment, &SectData);
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000301 SD.setFragment(F);
302
303 Symbol->setSection(*Section);
304
305 // Update the maximum alignment on the zero fill section if necessary.
306 if (ByteAlignment > SectData.getAlignment())
307 SectData.setAlignment(ByteAlignment);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000308}
309
Chris Lattneraaec2052010-01-19 19:46:13 +0000310void MCMachOStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000311 MCDataFragment *DF = dyn_cast_or_null<MCDataFragment>(getCurrentFragment());
312 if (!DF)
313 DF = new MCDataFragment(CurSectionData);
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000314 DF->getContents().append(Data.begin(), Data.end());
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000315}
316
Chris Lattneraaec2052010-01-19 19:46:13 +0000317void MCMachOStreamer::EmitValue(const MCExpr *Value, unsigned Size,
318 unsigned AddrSpace) {
Daniel Dunbar45f48742010-02-13 09:28:22 +0000319 MCDataFragment *DF = dyn_cast_or_null<MCDataFragment>(getCurrentFragment());
320 if (!DF)
321 DF = new MCDataFragment(CurSectionData);
322
323 // Avoid fixups when possible.
324 int64_t AbsValue;
Daniel Dunbar40ebe2472010-02-22 22:08:57 +0000325 if (AddValueSymbols(Value)->EvaluateAsAbsolute(AbsValue)) {
Daniel Dunbar45f48742010-02-13 09:28:22 +0000326 // FIXME: Endianness assumption.
327 for (unsigned i = 0; i != Size; ++i)
328 DF->getContents().push_back(uint8_t(AbsValue >> (i * 8)));
329 } else {
Daniel Dunbar8315a352010-03-12 21:00:38 +0000330 DF->addFixup(MCAsmFixup(DF->getContents().size(), *AddValueSymbols(Value),
331 MCFixup::getKindForSize(Size)));
Daniel Dunbar45f48742010-02-13 09:28:22 +0000332 DF->getContents().resize(DF->getContents().size() + Size, 0);
333 }
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000334}
335
336void MCMachOStreamer::EmitValueToAlignment(unsigned ByteAlignment,
337 int64_t Value, unsigned ValueSize,
338 unsigned MaxBytesToEmit) {
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000339 if (MaxBytesToEmit == 0)
340 MaxBytesToEmit = ByteAlignment;
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000341 new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit,
Kevin Enderby6e720482010-02-23 18:26:34 +0000342 false /* EmitNops */, CurSectionData);
343
344 // Update the maximum alignment on the current section if necessary.
345 if (ByteAlignment > CurSectionData->getAlignment())
346 CurSectionData->setAlignment(ByteAlignment);
347}
348
349void MCMachOStreamer::EmitCodeAlignment(unsigned ByteAlignment,
350 unsigned MaxBytesToEmit) {
351 if (MaxBytesToEmit == 0)
352 MaxBytesToEmit = ByteAlignment;
353 // FIXME the 0x90 is the default x86 1 byte nop opcode.
354 new MCAlignFragment(ByteAlignment, 0x90, 1, MaxBytesToEmit,
355 true /* EmitNops */, CurSectionData);
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000356
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000357 // Update the maximum alignment on the current section if necessary.
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000358 if (ByteAlignment > CurSectionData->getAlignment())
359 CurSectionData->setAlignment(ByteAlignment);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000360}
361
Daniel Dunbar821e3332009-08-31 08:09:28 +0000362void MCMachOStreamer::EmitValueToOffset(const MCExpr *Offset,
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000363 unsigned char Value) {
Daniel Dunbar1253a6f2009-10-16 01:58:03 +0000364 new MCOrgFragment(*Offset, Value, CurSectionData);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000365}
366
367void MCMachOStreamer::EmitInstruction(const MCInst &Inst) {
Daniel Dunbar4fac7492009-08-27 08:17:51 +0000368 // Scan for values.
369 for (unsigned i = 0; i != Inst.getNumOperands(); ++i)
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000370 if (Inst.getOperand(i).isExpr())
371 AddValueSymbols(Inst.getOperand(i).getExpr());
Daniel Dunbar4fac7492009-08-27 08:17:51 +0000372
373 if (!Emitter)
374 llvm_unreachable("no code emitter available!");
375
Daniel Dunbare1ec6172010-02-02 21:44:01 +0000376 CurSectionData->setHasInstructions(true);
377
Daniel Dunbar73c55742010-02-09 22:59:55 +0000378 SmallVector<MCFixup, 4> Fixups;
Daniel Dunbar4fac7492009-08-27 08:17:51 +0000379 SmallString<256> Code;
380 raw_svector_ostream VecOS(Code);
Daniel Dunbar73c55742010-02-09 22:59:55 +0000381 Emitter->EncodeInstruction(Inst, VecOS, Fixups);
Daniel Dunbarf6346762010-02-13 09:29:02 +0000382 VecOS.flush();
383
384 // Add the fixups and data.
385 MCDataFragment *DF = dyn_cast_or_null<MCDataFragment>(getCurrentFragment());
386 if (!DF)
387 DF = new MCDataFragment(CurSectionData);
388 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
389 MCFixup &F = Fixups[i];
Daniel Dunbar8315a352010-03-12 21:00:38 +0000390 DF->addFixup(MCAsmFixup(DF->getContents().size()+F.getOffset(),
391 *F.getValue(), F.getKind()));
Daniel Dunbarf6346762010-02-13 09:29:02 +0000392 }
393 DF->getContents().append(Code.begin(), Code.end());
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000394}
395
396void MCMachOStreamer::Finish() {
397 Assembler.Finish();
398}
399
Daniel Dunbar1f3e4452010-03-11 01:34:27 +0000400MCStreamer *llvm::createMachOStreamer(MCContext &Context, TargetAsmBackend &TAB,
401 raw_ostream &OS, MCCodeEmitter *CE) {
402 return new MCMachOStreamer(Context, TAB, OS, CE);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000403}