blob: 60b6c7c4e1aa84b11d669893d62bac9b67106d8f [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"
Kevin Enderbya6eeb6e2010-05-07 21:44:23 +000019#include "llvm/MC/MCMachOSymbolFlags.h"
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000020#include "llvm/Support/ErrorHandling.h"
Daniel Dunbar4fac7492009-08-27 08:17:51 +000021#include "llvm/Support/raw_ostream.h"
Daniel Dunbard8036fb2010-03-23 05:09:03 +000022#include "llvm/Target/TargetAsmBackend.h"
23
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000024using namespace llvm;
25
26namespace {
27
28class MCMachOStreamer : public MCStreamer {
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +000029
30private:
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000031 MCAssembler Assembler;
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000032 MCSectionData *CurSectionData;
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +000033
34private:
35 MCFragment *getCurrentFragment() const {
36 assert(CurSectionData && "No current section!");
37
38 if (!CurSectionData->empty())
39 return &CurSectionData->getFragmentList().back();
40
41 return 0;
42 }
43
Daniel Dunbarf70f4772010-03-22 20:35:46 +000044 /// Get a data fragment to write into, creating a new one if the current
45 /// fragment is not a data fragment.
46 MCDataFragment *getOrCreateDataFragment() const {
47 MCDataFragment *F = dyn_cast_or_null<MCDataFragment>(getCurrentFragment());
48 if (!F)
49 F = new MCDataFragment(CurSectionData);
50 return F;
51 }
52
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000053public:
Daniel Dunbar1f3e4452010-03-11 01:34:27 +000054 MCMachOStreamer(MCContext &Context, TargetAsmBackend &TAB,
55 raw_ostream &_OS, MCCodeEmitter *_Emitter)
Daniel Dunbarcf871e52010-03-19 10:43:18 +000056 : MCStreamer(Context), Assembler(Context, TAB, *_Emitter, _OS),
Daniel Dunbar4fac7492009-08-27 08:17:51 +000057 CurSectionData(0) {}
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000058 ~MCMachOStreamer() {}
59
Daniel Dunbarac2884a2010-03-25 22:49:09 +000060 MCAssembler &getAssembler() { return Assembler; }
61
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +000062 const MCExpr *AddValueSymbols(const MCExpr *Value) {
63 switch (Value->getKind()) {
Chris Lattner5d917a82010-02-08 19:41:07 +000064 case MCExpr::Target: assert(0 && "Can't handle target exprs yet!");
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +000065 case MCExpr::Constant:
66 break;
67
68 case MCExpr::Binary: {
69 const MCBinaryExpr *BE = cast<MCBinaryExpr>(Value);
70 AddValueSymbols(BE->getLHS());
71 AddValueSymbols(BE->getRHS());
72 break;
73 }
74
75 case MCExpr::SymbolRef:
Daniel Dunbar46836a72010-03-10 20:58:29 +000076 Assembler.getOrCreateSymbolData(
77 cast<MCSymbolRefExpr>(Value)->getSymbol());
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +000078 break;
79
80 case MCExpr::Unary:
81 AddValueSymbols(cast<MCUnaryExpr>(Value)->getSubExpr());
82 break;
83 }
84
85 return Value;
86 }
87
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000088 /// @name MCStreamer Interface
89 /// @{
90
91 virtual void SwitchSection(const MCSection *Section);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000092 virtual void EmitLabel(MCSymbol *Symbol);
Chris Lattnera5ad93a2010-01-23 06:39:22 +000093 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag);
Daniel Dunbar821e3332009-08-31 08:09:28 +000094 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value);
Chris Lattnera5ad93a2010-01-23 06:39:22 +000095 virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000096 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
Chris Lattner9eb158d2010-01-23 07:47:02 +000097 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +000098 unsigned ByteAlignment);
Chris Lattnerb54b9dd2010-05-08 19:54:22 +000099 virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol) {
100 assert(0 && "macho doesn't support this directive");
101 }
102 virtual void EmitCOFFSymbolStorageClass(int StorageClass) {
103 assert(0 && "macho doesn't support this directive");
104 }
105 virtual void EmitCOFFSymbolType(int Type) {
106 assert(0 && "macho doesn't support this directive");
107 }
108 virtual void EndCOFFSymbolDef() {
109 assert(0 && "macho doesn't support this directive");
110 }
Chris Lattner99328ad2010-01-25 07:52:13 +0000111 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
112 assert(0 && "macho doesn't support this directive");
113 }
Chris Lattner9eb158d2010-01-23 07:47:02 +0000114 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size) {
115 assert(0 && "macho doesn't support this directive");
116 }
Daniel Dunbar8751b942009-08-28 05:48:22 +0000117 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000118 unsigned Size = 0, unsigned ByteAlignment = 0);
Chris Lattneraaec2052010-01-19 19:46:13 +0000119 virtual void EmitBytes(StringRef Data, unsigned AddrSpace);
Chris Lattneraaec2052010-01-19 19:46:13 +0000120 virtual void EmitValue(const MCExpr *Value, unsigned Size,unsigned AddrSpace);
Chris Lattner718fb592010-01-25 21:28:50 +0000121 virtual void EmitGPRel32Value(const MCExpr *Value) {
122 assert(0 && "macho doesn't support this directive");
123 }
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000124 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
125 unsigned ValueSize = 1,
126 unsigned MaxBytesToEmit = 0);
Kevin Enderby6e720482010-02-23 18:26:34 +0000127 virtual void EmitCodeAlignment(unsigned ByteAlignment,
128 unsigned MaxBytesToEmit = 0);
Daniel Dunbar821e3332009-08-31 08:09:28 +0000129 virtual void EmitValueToOffset(const MCExpr *Offset,
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000130 unsigned char Value = 0);
Chris Lattnera6594fc2010-01-25 18:58:59 +0000131
132 virtual void EmitFileDirective(StringRef Filename) {
133 errs() << "FIXME: MCMachoStreamer:EmitFileDirective not implemented\n";
134 }
135 virtual void EmitDwarfFileDirective(unsigned FileNo, StringRef Filename) {
136 errs() << "FIXME: MCMachoStreamer:EmitDwarfFileDirective not implemented\n";
137 }
138
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000139 virtual void EmitInstruction(const MCInst &Inst);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000140 virtual void Finish();
141
142 /// @}
143};
144
145} // end anonymous namespace.
146
147void MCMachOStreamer::SwitchSection(const MCSection *Section) {
148 assert(Section && "Cannot switch to a null section!");
Chris Lattnercda7f782009-08-22 19:35:08 +0000149
150 // If already in this section, then this is a noop.
151 if (Section == CurSection) return;
Daniel Dunbar0f5fa692009-08-28 05:48:54 +0000152
Chris Lattnercda7f782009-08-22 19:35:08 +0000153 CurSection = Section;
Daniel Dunbar46836a72010-03-10 20:58:29 +0000154 CurSectionData = &Assembler.getOrCreateSectionData(*Section);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000155}
156
157void MCMachOStreamer::EmitLabel(MCSymbol *Symbol) {
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000158 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
Daniel Dunbarc3047182010-05-05 19:01:00 +0000159 assert(!Symbol->isVariable() && "Cannot emit a variable symbol!");
160 assert(CurSection && "Cannot emit before setting section!");
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 Dunbara4d86672009-10-16 01:58:23 +0000189 // FIXME: Lift context changes into super class.
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000190 Symbol->setVariableValue(AddValueSymbols(Value));
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000191}
192
193void MCMachOStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000194 MCSymbolAttr Attribute) {
Daniel Dunbar0c7761b2009-08-24 11:56:58 +0000195 // Indirect symbols are handled differently, to match how 'as' handles
196 // them. This makes writing matching .o files easier.
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000197 if (Attribute == MCSA_IndirectSymbol) {
Daniel Dunbarad7c3d52009-08-26 00:18:21 +0000198 // Note that we intentionally cannot use the symbol data here; this is
199 // important for matching the string table that 'as' generates.
Daniel Dunbar0c7761b2009-08-24 11:56:58 +0000200 IndirectSymbolData ISD;
201 ISD.Symbol = Symbol;
202 ISD.SectionData = CurSectionData;
203 Assembler.getIndirectSymbols().push_back(ISD);
204 return;
205 }
206
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000207 // Adding a symbol attribute always introduces the symbol, note that an
Daniel Dunbar46836a72010-03-10 20:58:29 +0000208 // important side effect of calling getOrCreateSymbolData here is to register
209 // the symbol with the assembler.
210 MCSymbolData &SD = Assembler.getOrCreateSymbolData(*Symbol);
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000211
212 // The implementation of symbol attributes is designed to match 'as', but it
213 // leaves much to desired. It doesn't really make sense to arbitrarily add and
214 // remove flags, but 'as' allows this (in particular, see .desc).
215 //
216 // In the future it might be worth trying to make these operations more well
217 // defined.
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000218 switch (Attribute) {
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000219 case MCSA_Invalid:
Chris Lattnered0ab152010-01-25 18:30:45 +0000220 case MCSA_ELF_TypeFunction:
221 case MCSA_ELF_TypeIndFunction:
222 case MCSA_ELF_TypeObject:
223 case MCSA_ELF_TypeTLS:
224 case MCSA_ELF_TypeCommon:
225 case MCSA_ELF_TypeNoType:
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000226 case MCSA_IndirectSymbol:
227 case MCSA_Hidden:
228 case MCSA_Internal:
229 case MCSA_Protected:
230 case MCSA_Weak:
231 case MCSA_Local:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000232 assert(0 && "Invalid symbol attribute for Mach-O!");
233 break;
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000234
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000235 case MCSA_Global:
Daniel Dunbar8f4d1462009-08-28 07:08:35 +0000236 SD.setExternal(true);
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000237 break;
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000238
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000239 case MCSA_LazyReference:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000240 // FIXME: This requires -dynamic.
241 SD.setFlags(SD.getFlags() | SF_NoDeadStrip);
242 if (Symbol->isUndefined())
243 SD.setFlags(SD.getFlags() | SF_ReferenceTypeUndefinedLazy);
244 break;
245
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000246 // Since .reference sets the no dead strip bit, it is equivalent to
247 // .no_dead_strip in practice.
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000248 case MCSA_Reference:
249 case MCSA_NoDeadStrip:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000250 SD.setFlags(SD.getFlags() | SF_NoDeadStrip);
251 break;
252
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000253 case MCSA_PrivateExtern:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000254 SD.setExternal(true);
255 SD.setPrivateExtern(true);
256 break;
257
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000258 case MCSA_WeakReference:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000259 // FIXME: This requires -dynamic.
260 if (Symbol->isUndefined())
261 SD.setFlags(SD.getFlags() | SF_WeakReference);
262 break;
263
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000264 case MCSA_WeakDefinition:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000265 // FIXME: 'as' enforces that this is defined and global. The manual claims
266 // it has to be in a coalesced section, but this isn't enforced.
267 SD.setFlags(SD.getFlags() | SF_WeakDefinition);
268 break;
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000269 }
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000270}
271
272void MCMachOStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000273 // Encode the 'desc' value into the lowest implementation defined bits.
274 assert(DescValue == (DescValue & SF_DescFlagsMask) &&
275 "Invalid .desc value!");
Daniel Dunbar46836a72010-03-10 20:58:29 +0000276 Assembler.getOrCreateSymbolData(*Symbol).setFlags(DescValue&SF_DescFlagsMask);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000277}
278
Chris Lattner9eb158d2010-01-23 07:47:02 +0000279void MCMachOStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000280 unsigned ByteAlignment) {
Daniel Dunbar8f4d1462009-08-28 07:08:35 +0000281 // FIXME: Darwin 'as' does appear to allow redef of a .comm by itself.
282 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
283
Daniel Dunbar46836a72010-03-10 20:58:29 +0000284 MCSymbolData &SD = Assembler.getOrCreateSymbolData(*Symbol);
Daniel Dunbar8f4d1462009-08-28 07:08:35 +0000285 SD.setExternal(true);
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000286 SD.setCommon(Size, ByteAlignment);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000287}
288
Daniel Dunbar8751b942009-08-28 05:48:22 +0000289void MCMachOStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000290 unsigned Size, unsigned ByteAlignment) {
Daniel Dunbar46836a72010-03-10 20:58:29 +0000291 MCSectionData &SectData = Assembler.getOrCreateSectionData(*Section);
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000292
293 // The symbol may not be present, which only creates the section.
294 if (!Symbol)
295 return;
296
297 // FIXME: Assert that this section has the zerofill type.
298
299 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
300
Daniel Dunbar46836a72010-03-10 20:58:29 +0000301 MCSymbolData &SD = Assembler.getOrCreateSymbolData(*Symbol);
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000302
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000303 MCFragment *F = new MCZeroFillFragment(Size, ByteAlignment, &SectData);
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000304 SD.setFragment(F);
305
306 Symbol->setSection(*Section);
307
308 // Update the maximum alignment on the zero fill section if necessary.
309 if (ByteAlignment > SectData.getAlignment())
310 SectData.setAlignment(ByteAlignment);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000311}
312
Chris Lattneraaec2052010-01-19 19:46:13 +0000313void MCMachOStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {
Daniel Dunbarf70f4772010-03-22 20:35:46 +0000314 getOrCreateDataFragment()->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 Dunbarf70f4772010-03-22 20:35:46 +0000319 MCDataFragment *DF = getOrCreateDataFragment();
Daniel Dunbar45f48742010-02-13 09:28:22 +0000320
321 // Avoid fixups when possible.
322 int64_t AbsValue;
Daniel Dunbar40ebe2472010-02-22 22:08:57 +0000323 if (AddValueSymbols(Value)->EvaluateAsAbsolute(AbsValue)) {
Daniel Dunbar45f48742010-02-13 09:28:22 +0000324 // FIXME: Endianness assumption.
325 for (unsigned i = 0; i != Size; ++i)
326 DF->getContents().push_back(uint8_t(AbsValue >> (i * 8)));
327 } else {
Daniel Dunbar8315a352010-03-12 21:00:38 +0000328 DF->addFixup(MCAsmFixup(DF->getContents().size(), *AddValueSymbols(Value),
329 MCFixup::getKindForSize(Size)));
Daniel Dunbar45f48742010-02-13 09:28:22 +0000330 DF->getContents().resize(DF->getContents().size() + Size, 0);
331 }
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000332}
333
334void MCMachOStreamer::EmitValueToAlignment(unsigned ByteAlignment,
335 int64_t Value, unsigned ValueSize,
336 unsigned MaxBytesToEmit) {
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000337 if (MaxBytesToEmit == 0)
338 MaxBytesToEmit = ByteAlignment;
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000339 new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit,
Kevin Enderby6e720482010-02-23 18:26:34 +0000340 false /* EmitNops */, CurSectionData);
341
342 // Update the maximum alignment on the current section if necessary.
343 if (ByteAlignment > CurSectionData->getAlignment())
344 CurSectionData->setAlignment(ByteAlignment);
345}
346
347void MCMachOStreamer::EmitCodeAlignment(unsigned ByteAlignment,
348 unsigned MaxBytesToEmit) {
349 if (MaxBytesToEmit == 0)
350 MaxBytesToEmit = ByteAlignment;
Daniel Dunbar8f9b80e2010-03-23 02:36:58 +0000351 new MCAlignFragment(ByteAlignment, 0, 1, MaxBytesToEmit,
Kevin Enderby6e720482010-02-23 18:26:34 +0000352 true /* EmitNops */, CurSectionData);
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000353
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000354 // Update the maximum alignment on the current section if necessary.
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000355 if (ByteAlignment > CurSectionData->getAlignment())
356 CurSectionData->setAlignment(ByteAlignment);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000357}
358
Daniel Dunbar821e3332009-08-31 08:09:28 +0000359void MCMachOStreamer::EmitValueToOffset(const MCExpr *Offset,
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000360 unsigned char Value) {
Daniel Dunbar1253a6f2009-10-16 01:58:03 +0000361 new MCOrgFragment(*Offset, Value, CurSectionData);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000362}
363
364void MCMachOStreamer::EmitInstruction(const MCInst &Inst) {
Daniel Dunbar4fac7492009-08-27 08:17:51 +0000365 // Scan for values.
366 for (unsigned i = 0; i != Inst.getNumOperands(); ++i)
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000367 if (Inst.getOperand(i).isExpr())
368 AddValueSymbols(Inst.getOperand(i).getExpr());
Daniel Dunbar4fac7492009-08-27 08:17:51 +0000369
Daniel Dunbare1ec6172010-02-02 21:44:01 +0000370 CurSectionData->setHasInstructions(true);
371
Daniel Dunbar3f4dcd92010-03-22 23:16:48 +0000372 // FIXME-PERF: Common case is that we don't need to relax, encode directly
373 // onto the data fragments buffers.
374
Daniel Dunbar73c55742010-02-09 22:59:55 +0000375 SmallVector<MCFixup, 4> Fixups;
Daniel Dunbar4fac7492009-08-27 08:17:51 +0000376 SmallString<256> Code;
377 raw_svector_ostream VecOS(Code);
Daniel Dunbarcf871e52010-03-19 10:43:18 +0000378 Assembler.getEmitter().EncodeInstruction(Inst, VecOS, Fixups);
Daniel Dunbarf6346762010-02-13 09:29:02 +0000379 VecOS.flush();
380
Daniel Dunbar337055e2010-03-23 03:13:05 +0000381 // FIXME: Eliminate this copy.
382 SmallVector<MCAsmFixup, 4> AsmFixups;
Daniel Dunbarf6346762010-02-13 09:29:02 +0000383 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
384 MCFixup &F = Fixups[i];
Daniel Dunbar337055e2010-03-23 03:13:05 +0000385 AsmFixups.push_back(MCAsmFixup(F.getOffset(), *F.getValue(),
386 F.getKind()));
387 }
388
Daniel Dunbard8036fb2010-03-23 05:09:03 +0000389 // See if we might need to relax this instruction, if so it needs its own
390 // fragment.
391 //
392 // FIXME-PERF: Support target hook to do a fast path that avoids the encoder,
393 // when we can immediately tell that we will get something which might need
394 // relaxation (and compute its size).
395 //
396 // FIXME-PERF: We should also be smart about immediately relaxing instructions
397 // which we can already show will never possibly fit (we can also do a very
398 // good job of this before we do the first relaxation pass, because we have
399 // total knowledge about undefined symbols at that point). Even now, though,
400 // we can do a decent job, especially on Darwin where scattering means that we
401 // are going to often know that we can never fully resolve a fixup.
402 if (Assembler.getBackend().MayNeedRelaxation(Inst, AsmFixups)) {
403 MCInstFragment *IF = new MCInstFragment(Inst, CurSectionData);
404
405 // Add the fixups and data.
406 //
407 // FIXME: Revisit this design decision when relaxation is done, we may be
408 // able to get away with not storing any extra data in the MCInst.
409 IF->getCode() = Code;
410 IF->getFixups() = AsmFixups;
411
412 return;
413 }
414
Daniel Dunbar337055e2010-03-23 03:13:05 +0000415 // Add the fixups and data.
416 MCDataFragment *DF = getOrCreateDataFragment();
417 for (unsigned i = 0, e = AsmFixups.size(); i != e; ++i) {
418 AsmFixups[i].Offset += DF->getContents().size();
419 DF->addFixup(AsmFixups[i]);
Daniel Dunbarf6346762010-02-13 09:29:02 +0000420 }
421 DF->getContents().append(Code.begin(), Code.end());
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000422}
423
424void MCMachOStreamer::Finish() {
425 Assembler.Finish();
426}
427
Daniel Dunbar1f3e4452010-03-11 01:34:27 +0000428MCStreamer *llvm::createMachOStreamer(MCContext &Context, TargetAsmBackend &TAB,
Daniel Dunbarac2884a2010-03-25 22:49:09 +0000429 raw_ostream &OS, MCCodeEmitter *CE,
430 bool RelaxAll) {
431 MCMachOStreamer *S = new MCMachOStreamer(Context, TAB, OS, CE);
432 if (RelaxAll)
433 S->getAssembler().setRelaxAll(true);
434 return S;
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000435}