blob: d0ea3b6780681b1e2d2f5c2a518daeb674008dff [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 Dunbar8dc68ab2010-06-16 20:04:22 +000017#include "llvm/MC/MCObjectStreamer.h"
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000018#include "llvm/MC/MCSection.h"
19#include "llvm/MC/MCSymbol.h"
Kevin Enderbya6eeb6e2010-05-07 21:44:23 +000020#include "llvm/MC/MCMachOSymbolFlags.h"
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000021#include "llvm/Support/ErrorHandling.h"
Daniel Dunbar4fac7492009-08-27 08:17:51 +000022#include "llvm/Support/raw_ostream.h"
Daniel Dunbard8036fb2010-03-23 05:09:03 +000023#include "llvm/Target/TargetAsmBackend.h"
24
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000025using namespace llvm;
26
27namespace {
28
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +000029class MCMachOStreamer : public MCObjectStreamer {
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +000030private:
Daniel Dunbar071f73d2010-05-10 22:45:09 +000031 /// Track the current atom for each section.
32 DenseMap<const MCSectionData*, MCSymbolData*> CurrentAtomMap;
33
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +000034private:
35 MCFragment *getCurrentFragment() const {
Daniel Dunbar83b46712010-06-16 20:04:25 +000036 assert(getCurrentSectionData() && "No current section!");
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +000037
Daniel Dunbar83b46712010-06-16 20:04:25 +000038 if (!getCurrentSectionData()->empty())
39 return &getCurrentSectionData()->getFragmentList().back();
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +000040
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)
Daniel Dunbar071f73d2010-05-10 22:45:09 +000049 F = createDataFragment();
Daniel Dunbarf70f4772010-03-22 20:35:46 +000050 return F;
51 }
52
Daniel Dunbar071f73d2010-05-10 22:45:09 +000053 /// Create a new data fragment in the current section.
54 MCDataFragment *createDataFragment() const {
Daniel Dunbar83b46712010-06-16 20:04:25 +000055 MCDataFragment *DF = new MCDataFragment(getCurrentSectionData());
56 DF->setAtom(CurrentAtomMap.lookup(getCurrentSectionData()));
Daniel Dunbar071f73d2010-05-10 22:45:09 +000057 return DF;
58 }
59
Daniel Dunbar2ac0c452010-05-26 20:37:00 +000060 void EmitInstToFragment(const MCInst &Inst);
61 void EmitInstToData(const MCInst &Inst);
62
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000063public:
Daniel Dunbar1f3e4452010-03-11 01:34:27 +000064 MCMachOStreamer(MCContext &Context, TargetAsmBackend &TAB,
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +000065 raw_ostream &OS, MCCodeEmitter *Emitter)
Daniel Dunbar83b46712010-06-16 20:04:25 +000066 : MCObjectStreamer(Context, TAB, OS, Emitter) {}
Daniel Dunbarac2884a2010-03-25 22:49:09 +000067
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 Dunbar8dc68ab2010-06-16 20:04:22 +000082 getAssembler().getOrCreateSymbolData(
Daniel Dunbar46836a72010-03-10 20:58:29 +000083 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
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 Lattnerb54b9dd2010-05-08 19:54:22 +0000104 virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol) {
105 assert(0 && "macho doesn't support this directive");
106 }
107 virtual void EmitCOFFSymbolStorageClass(int StorageClass) {
108 assert(0 && "macho doesn't support this directive");
109 }
110 virtual void EmitCOFFSymbolType(int Type) {
111 assert(0 && "macho doesn't support this directive");
112 }
113 virtual void EndCOFFSymbolDef() {
114 assert(0 && "macho doesn't support this directive");
115 }
Chris Lattner99328ad2010-01-25 07:52:13 +0000116 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
117 assert(0 && "macho doesn't support this directive");
118 }
Chris Lattner9eb158d2010-01-23 07:47:02 +0000119 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size) {
120 assert(0 && "macho doesn't support this directive");
121 }
Daniel Dunbar8751b942009-08-28 05:48:22 +0000122 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000123 unsigned Size = 0, unsigned ByteAlignment = 0);
Eric Christopher011c3f12010-05-18 21:26:41 +0000124 virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
125 uint64_t Size, unsigned ByteAlignment = 0);
Chris Lattneraaec2052010-01-19 19:46:13 +0000126 virtual void EmitBytes(StringRef Data, unsigned AddrSpace);
Chris Lattneraaec2052010-01-19 19:46:13 +0000127 virtual void EmitValue(const MCExpr *Value, unsigned Size,unsigned AddrSpace);
Chris Lattner718fb592010-01-25 21:28:50 +0000128 virtual void EmitGPRel32Value(const MCExpr *Value) {
129 assert(0 && "macho doesn't support this directive");
130 }
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000131 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
132 unsigned ValueSize = 1,
133 unsigned MaxBytesToEmit = 0);
Kevin Enderby6e720482010-02-23 18:26:34 +0000134 virtual void EmitCodeAlignment(unsigned ByteAlignment,
135 unsigned MaxBytesToEmit = 0);
Daniel Dunbar821e3332009-08-31 08:09:28 +0000136 virtual void EmitValueToOffset(const MCExpr *Offset,
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000137 unsigned char Value = 0);
Daniel Dunbar2ac0c452010-05-26 20:37:00 +0000138
Chris Lattnera6594fc2010-01-25 18:58:59 +0000139 virtual void EmitFileDirective(StringRef Filename) {
Daniel Dunbarca1212d2010-05-18 17:28:17 +0000140 report_fatal_error("unsupported directive: '.file'");
Chris Lattnera6594fc2010-01-25 18:58:59 +0000141 }
142 virtual void EmitDwarfFileDirective(unsigned FileNo, StringRef Filename) {
Daniel Dunbarca1212d2010-05-18 17:28:17 +0000143 report_fatal_error("unsupported directive: '.file'");
Chris Lattnera6594fc2010-01-25 18:58:59 +0000144 }
Daniel Dunbar2ac0c452010-05-26 20:37:00 +0000145
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000146 virtual void EmitInstruction(const MCInst &Inst);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000147
148 /// @}
149};
150
151} // end anonymous namespace.
152
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000153void MCMachOStreamer::EmitLabel(MCSymbol *Symbol) {
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000154 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
Daniel Dunbarc3047182010-05-05 19:01:00 +0000155 assert(!Symbol->isVariable() && "Cannot emit a variable symbol!");
156 assert(CurSection && "Cannot emit before setting section!");
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000157
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000158 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
Daniel Dunbar071f73d2010-05-10 22:45:09 +0000159
160 // Update the current atom map, if necessary.
161 bool MustCreateFragment = false;
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000162 if (getAssembler().isSymbolLinkerVisible(&SD)) {
Daniel Dunbar83b46712010-06-16 20:04:25 +0000163 CurrentAtomMap[getCurrentSectionData()] = &SD;
Daniel Dunbar071f73d2010-05-10 22:45:09 +0000164
165 // We have to create a new fragment, fragments cannot span atoms.
166 MustCreateFragment = true;
167 }
168
Daniel Dunbarf70f4772010-03-22 20:35:46 +0000169 // FIXME: This is wasteful, we don't necessarily need to create a data
170 // fragment. Instead, we should mark the symbol as pointing into the data
171 // fragment if it exists, otherwise we should just queue the label and set its
172 // fragment pointer when we emit the next fragment.
Daniel Dunbar071f73d2010-05-10 22:45:09 +0000173 MCDataFragment *F =
174 MustCreateFragment ? createDataFragment() : getOrCreateDataFragment();
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000175 assert(!SD.getFragment() && "Unexpected fragment on symbol data!");
176 SD.setFragment(F);
177 SD.setOffset(F->getContents().size());
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000178
Daniel Dunbarb18d2dd2010-05-17 20:12:31 +0000179 // This causes the reference type flag to be cleared. Darwin 'as' was "trying"
180 // to clear the weak reference and weak definition bits too, but the
181 // implementation was buggy. For now we just try to match 'as', for
182 // diffability.
183 //
184 // FIXME: Cleanup this code, these bits should be emitted based on semantic
185 // properties, not on the order of definition, etc.
186 SD.setFlags(SD.getFlags() & ~SF_ReferenceTypeMask);
Daniel Dunbar2ac0c452010-05-26 20:37:00 +0000187
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000188 Symbol->setSection(*CurSection);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000189}
190
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000191void MCMachOStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
Daniel Dunbar6009db42009-08-26 21:22:22 +0000192 switch (Flag) {
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000193 case MCAF_SubsectionsViaSymbols:
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000194 getAssembler().setSubsectionsViaSymbols(true);
Daniel Dunbar8c3eaf42009-08-28 07:08:47 +0000195 return;
Daniel Dunbar6009db42009-08-26 21:22:22 +0000196 }
Daniel Dunbar8c3eaf42009-08-28 07:08:47 +0000197
198 assert(0 && "invalid assembler flag!");
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000199}
200
Daniel Dunbar821e3332009-08-31 08:09:28 +0000201void MCMachOStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
Daniel Dunbara4d86672009-10-16 01:58:23 +0000202 // FIXME: Lift context changes into super class.
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000203 getAssembler().getOrCreateSymbolData(*Symbol);
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000204 Symbol->setVariableValue(AddValueSymbols(Value));
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000205}
206
207void MCMachOStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000208 MCSymbolAttr Attribute) {
Daniel Dunbar0c7761b2009-08-24 11:56:58 +0000209 // Indirect symbols are handled differently, to match how 'as' handles
210 // them. This makes writing matching .o files easier.
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000211 if (Attribute == MCSA_IndirectSymbol) {
Daniel Dunbarad7c3d52009-08-26 00:18:21 +0000212 // Note that we intentionally cannot use the symbol data here; this is
213 // important for matching the string table that 'as' generates.
Daniel Dunbar0c7761b2009-08-24 11:56:58 +0000214 IndirectSymbolData ISD;
215 ISD.Symbol = Symbol;
Daniel Dunbar83b46712010-06-16 20:04:25 +0000216 ISD.SectionData = getCurrentSectionData();
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000217 getAssembler().getIndirectSymbols().push_back(ISD);
Daniel Dunbar0c7761b2009-08-24 11:56:58 +0000218 return;
219 }
220
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000221 // Adding a symbol attribute always introduces the symbol, note that an
Daniel Dunbar46836a72010-03-10 20:58:29 +0000222 // important side effect of calling getOrCreateSymbolData here is to register
223 // the symbol with the assembler.
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000224 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000225
226 // The implementation of symbol attributes is designed to match 'as', but it
227 // leaves much to desired. It doesn't really make sense to arbitrarily add and
228 // remove flags, but 'as' allows this (in particular, see .desc).
229 //
230 // In the future it might be worth trying to make these operations more well
231 // defined.
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000232 switch (Attribute) {
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000233 case MCSA_Invalid:
Chris Lattnered0ab152010-01-25 18:30:45 +0000234 case MCSA_ELF_TypeFunction:
235 case MCSA_ELF_TypeIndFunction:
236 case MCSA_ELF_TypeObject:
237 case MCSA_ELF_TypeTLS:
238 case MCSA_ELF_TypeCommon:
239 case MCSA_ELF_TypeNoType:
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000240 case MCSA_IndirectSymbol:
241 case MCSA_Hidden:
242 case MCSA_Internal:
243 case MCSA_Protected:
244 case MCSA_Weak:
245 case MCSA_Local:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000246 assert(0 && "Invalid symbol attribute for Mach-O!");
247 break;
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000248
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000249 case MCSA_Global:
Daniel Dunbar8f4d1462009-08-28 07:08:35 +0000250 SD.setExternal(true);
Daniel Dunbarb18d2dd2010-05-17 20:12:31 +0000251 // This effectively clears the undefined lazy bit, in Darwin 'as', although
252 // it isn't very consistent because it implements this as part of symbol
253 // lookup.
254 //
255 // FIXME: Cleanup this code, these bits should be emitted based on semantic
256 // properties, not on the order of definition, etc.
257 SD.setFlags(SD.getFlags() & ~SF_ReferenceTypeUndefinedLazy);
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000258 break;
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000259
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000260 case MCSA_LazyReference:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000261 // FIXME: This requires -dynamic.
262 SD.setFlags(SD.getFlags() | SF_NoDeadStrip);
263 if (Symbol->isUndefined())
264 SD.setFlags(SD.getFlags() | SF_ReferenceTypeUndefinedLazy);
265 break;
266
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000267 // Since .reference sets the no dead strip bit, it is equivalent to
268 // .no_dead_strip in practice.
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000269 case MCSA_Reference:
270 case MCSA_NoDeadStrip:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000271 SD.setFlags(SD.getFlags() | SF_NoDeadStrip);
272 break;
273
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000274 case MCSA_PrivateExtern:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000275 SD.setExternal(true);
276 SD.setPrivateExtern(true);
277 break;
278
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000279 case MCSA_WeakReference:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000280 // FIXME: This requires -dynamic.
281 if (Symbol->isUndefined())
282 SD.setFlags(SD.getFlags() | SF_WeakReference);
283 break;
284
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000285 case MCSA_WeakDefinition:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000286 // FIXME: 'as' enforces that this is defined and global. The manual claims
287 // it has to be in a coalesced section, but this isn't enforced.
288 SD.setFlags(SD.getFlags() | SF_WeakDefinition);
289 break;
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000290 }
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000291}
292
293void MCMachOStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000294 // Encode the 'desc' value into the lowest implementation defined bits.
Daniel Dunbar2ac0c452010-05-26 20:37:00 +0000295 assert(DescValue == (DescValue & SF_DescFlagsMask) &&
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000296 "Invalid .desc value!");
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000297 getAssembler().getOrCreateSymbolData(*Symbol).setFlags(
298 DescValue & SF_DescFlagsMask);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000299}
300
Chris Lattner9eb158d2010-01-23 07:47:02 +0000301void MCMachOStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000302 unsigned ByteAlignment) {
Daniel Dunbar8f4d1462009-08-28 07:08:35 +0000303 // FIXME: Darwin 'as' does appear to allow redef of a .comm by itself.
304 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
305
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000306 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
Daniel Dunbar8f4d1462009-08-28 07:08:35 +0000307 SD.setExternal(true);
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000308 SD.setCommon(Size, ByteAlignment);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000309}
310
Daniel Dunbar8751b942009-08-28 05:48:22 +0000311void MCMachOStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000312 unsigned Size, unsigned ByteAlignment) {
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000313 MCSectionData &SectData = getAssembler().getOrCreateSectionData(*Section);
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000314
315 // The symbol may not be present, which only creates the section.
316 if (!Symbol)
317 return;
318
319 // FIXME: Assert that this section has the zerofill type.
320
321 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
322
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000323 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000324
Daniel Dunbare73d49e2010-05-12 22:51:27 +0000325 // Emit an align fragment if necessary.
326 if (ByteAlignment != 1)
Daniel Dunbar1c154132010-05-12 22:56:23 +0000327 new MCAlignFragment(ByteAlignment, 0, 0, ByteAlignment, &SectData);
Daniel Dunbare73d49e2010-05-12 22:51:27 +0000328
Daniel Dunbar4e544872010-05-12 22:51:38 +0000329 MCFragment *F = new MCFillFragment(0, 0, Size, &SectData);
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000330 SD.setFragment(F);
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000331 if (getAssembler().isSymbolLinkerVisible(&SD))
Daniel Dunbar071f73d2010-05-10 22:45:09 +0000332 F->setAtom(&SD);
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000333
334 Symbol->setSection(*Section);
335
336 // Update the maximum alignment on the zero fill section if necessary.
337 if (ByteAlignment > SectData.getAlignment())
338 SectData.setAlignment(ByteAlignment);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000339}
340
Eric Christopher070c1ab2010-05-21 23:03:53 +0000341// This should always be called with the thread local bss section. Like the
342// .zerofill directive this doesn't actually switch sections on us.
Eric Christopher011c3f12010-05-18 21:26:41 +0000343void MCMachOStreamer::EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
344 uint64_t Size, unsigned ByteAlignment) {
345 EmitZerofill(Section, Symbol, Size, ByteAlignment);
346 return;
Eric Christopher482eba02010-05-14 01:50:28 +0000347}
348
Chris Lattneraaec2052010-01-19 19:46:13 +0000349void MCMachOStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {
Daniel Dunbarf70f4772010-03-22 20:35:46 +0000350 getOrCreateDataFragment()->getContents().append(Data.begin(), Data.end());
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000351}
352
Chris Lattneraaec2052010-01-19 19:46:13 +0000353void MCMachOStreamer::EmitValue(const MCExpr *Value, unsigned Size,
354 unsigned AddrSpace) {
Daniel Dunbarf70f4772010-03-22 20:35:46 +0000355 MCDataFragment *DF = getOrCreateDataFragment();
Daniel Dunbar45f48742010-02-13 09:28:22 +0000356
357 // Avoid fixups when possible.
358 int64_t AbsValue;
Daniel Dunbar40ebe2472010-02-22 22:08:57 +0000359 if (AddValueSymbols(Value)->EvaluateAsAbsolute(AbsValue)) {
Daniel Dunbar45f48742010-02-13 09:28:22 +0000360 // FIXME: Endianness assumption.
361 for (unsigned i = 0; i != Size; ++i)
362 DF->getContents().push_back(uint8_t(AbsValue >> (i * 8)));
363 } else {
Daniel Dunbarc90e30a2010-05-26 15:18:56 +0000364 DF->addFixup(MCFixup::Create(DF->getContents().size(),
365 AddValueSymbols(Value),
366 MCFixup::getKindForSize(Size)));
Daniel Dunbar45f48742010-02-13 09:28:22 +0000367 DF->getContents().resize(DF->getContents().size() + Size, 0);
368 }
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000369}
370
371void MCMachOStreamer::EmitValueToAlignment(unsigned ByteAlignment,
372 int64_t Value, unsigned ValueSize,
373 unsigned MaxBytesToEmit) {
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000374 if (MaxBytesToEmit == 0)
375 MaxBytesToEmit = ByteAlignment;
Daniel Dunbar071f73d2010-05-10 22:45:09 +0000376 MCFragment *F = new MCAlignFragment(ByteAlignment, Value, ValueSize,
Daniel Dunbar83b46712010-06-16 20:04:25 +0000377 MaxBytesToEmit, getCurrentSectionData());
378 F->setAtom(CurrentAtomMap.lookup(getCurrentSectionData()));
Kevin Enderby6e720482010-02-23 18:26:34 +0000379
380 // Update the maximum alignment on the current section if necessary.
Daniel Dunbar83b46712010-06-16 20:04:25 +0000381 if (ByteAlignment > getCurrentSectionData()->getAlignment())
382 getCurrentSectionData()->setAlignment(ByteAlignment);
Kevin Enderby6e720482010-02-23 18:26:34 +0000383}
384
385void MCMachOStreamer::EmitCodeAlignment(unsigned ByteAlignment,
386 unsigned MaxBytesToEmit) {
387 if (MaxBytesToEmit == 0)
388 MaxBytesToEmit = ByteAlignment;
Daniel Dunbar1c154132010-05-12 22:56:23 +0000389 MCAlignFragment *F = new MCAlignFragment(ByteAlignment, 0, 1, MaxBytesToEmit,
Daniel Dunbar83b46712010-06-16 20:04:25 +0000390 getCurrentSectionData());
Daniel Dunbar1c154132010-05-12 22:56:23 +0000391 F->setEmitNops(true);
Daniel Dunbar83b46712010-06-16 20:04:25 +0000392 F->setAtom(CurrentAtomMap.lookup(getCurrentSectionData()));
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000393
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000394 // Update the maximum alignment on the current section if necessary.
Daniel Dunbar83b46712010-06-16 20:04:25 +0000395 if (ByteAlignment > getCurrentSectionData()->getAlignment())
396 getCurrentSectionData()->setAlignment(ByteAlignment);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000397}
398
Daniel Dunbar821e3332009-08-31 08:09:28 +0000399void MCMachOStreamer::EmitValueToOffset(const MCExpr *Offset,
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000400 unsigned char Value) {
Daniel Dunbar83b46712010-06-16 20:04:25 +0000401 MCFragment *F = new MCOrgFragment(*Offset, Value, getCurrentSectionData());
402 F->setAtom(CurrentAtomMap.lookup(getCurrentSectionData()));
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000403}
404
Daniel Dunbar2ac0c452010-05-26 20:37:00 +0000405void MCMachOStreamer::EmitInstToFragment(const MCInst &Inst) {
Daniel Dunbar83b46712010-06-16 20:04:25 +0000406 MCInstFragment *IF = new MCInstFragment(Inst, getCurrentSectionData());
407 IF->setAtom(CurrentAtomMap.lookup(getCurrentSectionData()));
Daniel Dunbar2ac0c452010-05-26 20:37:00 +0000408
409 // Add the fixups and data.
410 //
411 // FIXME: Revisit this design decision when relaxation is done, we may be
412 // able to get away with not storing any extra data in the MCInst.
413 SmallVector<MCFixup, 4> Fixups;
414 SmallString<256> Code;
415 raw_svector_ostream VecOS(Code);
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000416 getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups);
Daniel Dunbar2ac0c452010-05-26 20:37:00 +0000417 VecOS.flush();
418
419 IF->getCode() = Code;
420 IF->getFixups() = Fixups;
421}
422
423void MCMachOStreamer::EmitInstToData(const MCInst &Inst) {
424 MCDataFragment *DF = getOrCreateDataFragment();
425
426 SmallVector<MCFixup, 4> Fixups;
427 SmallString<256> Code;
428 raw_svector_ostream VecOS(Code);
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000429 getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups);
Daniel Dunbar2ac0c452010-05-26 20:37:00 +0000430 VecOS.flush();
431
432 // Add the fixups and data.
433 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
434 Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size());
435 DF->addFixup(Fixups[i]);
436 }
437 DF->getContents().append(Code.begin(), Code.end());
438}
439
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000440void MCMachOStreamer::EmitInstruction(const MCInst &Inst) {
Daniel Dunbar4fac7492009-08-27 08:17:51 +0000441 // Scan for values.
Daniel Dunbardb9014d2010-05-17 21:19:59 +0000442 for (unsigned i = Inst.getNumOperands(); i--; )
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000443 if (Inst.getOperand(i).isExpr())
444 AddValueSymbols(Inst.getOperand(i).getExpr());
Daniel Dunbar4fac7492009-08-27 08:17:51 +0000445
Daniel Dunbar83b46712010-06-16 20:04:25 +0000446 getCurrentSectionData()->setHasInstructions(true);
Daniel Dunbare1ec6172010-02-02 21:44:01 +0000447
Daniel Dunbar83194de2010-05-26 20:37:03 +0000448 // If this instruction doesn't need relaxation, just emit it as data.
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000449 if (!getAssembler().getBackend().MayNeedRelaxation(Inst)) {
Daniel Dunbar2ac0c452010-05-26 20:37:00 +0000450 EmitInstToData(Inst);
Daniel Dunbar83194de2010-05-26 20:37:03 +0000451 return;
452 }
453
454 // Otherwise, if we are relaxing everything, relax the instruction as much as
455 // possible and emit it as data.
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000456 if (getAssembler().getRelaxAll()) {
Daniel Dunbar83194de2010-05-26 20:37:03 +0000457 MCInst Relaxed;
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000458 getAssembler().getBackend().RelaxInstruction(Inst, Relaxed);
459 while (getAssembler().getBackend().MayNeedRelaxation(Relaxed))
460 getAssembler().getBackend().RelaxInstruction(Relaxed, Relaxed);
Daniel Dunbar83194de2010-05-26 20:37:03 +0000461 EmitInstToData(Relaxed);
462 return;
463 }
464
465 // Otherwise emit to a separate fragment.
466 EmitInstToFragment(Inst);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000467}
468
Daniel Dunbar1f3e4452010-03-11 01:34:27 +0000469MCStreamer *llvm::createMachOStreamer(MCContext &Context, TargetAsmBackend &TAB,
Daniel Dunbarac2884a2010-03-25 22:49:09 +0000470 raw_ostream &OS, MCCodeEmitter *CE,
471 bool RelaxAll) {
472 MCMachOStreamer *S = new MCMachOStreamer(Context, TAB, OS, CE);
473 if (RelaxAll)
474 S->getAssembler().setRelaxAll(true);
475 return S;
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000476}