blob: 27e4e98596e668adfa8809f2e27b916bc1f3e2cd [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
Daniel Dunbar071f73d2010-05-10 22:45:09 +000034 /// Track the current atom for each section.
35 DenseMap<const MCSectionData*, MCSymbolData*> CurrentAtomMap;
36
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +000037private:
38 MCFragment *getCurrentFragment() const {
39 assert(CurSectionData && "No current section!");
40
41 if (!CurSectionData->empty())
42 return &CurSectionData->getFragmentList().back();
43
44 return 0;
45 }
46
Daniel Dunbarf70f4772010-03-22 20:35:46 +000047 /// Get a data fragment to write into, creating a new one if the current
48 /// fragment is not a data fragment.
49 MCDataFragment *getOrCreateDataFragment() const {
50 MCDataFragment *F = dyn_cast_or_null<MCDataFragment>(getCurrentFragment());
51 if (!F)
Daniel Dunbar071f73d2010-05-10 22:45:09 +000052 F = createDataFragment();
Daniel Dunbarf70f4772010-03-22 20:35:46 +000053 return F;
54 }
55
Daniel Dunbar071f73d2010-05-10 22:45:09 +000056 /// Create a new data fragment in the current section.
57 MCDataFragment *createDataFragment() const {
58 MCDataFragment *DF = new MCDataFragment(CurSectionData);
59 DF->setAtom(CurrentAtomMap.lookup(CurSectionData));
60 return DF;
61 }
62
Daniel Dunbar2ac0c452010-05-26 20:37:00 +000063 void EmitInstToFragment(const MCInst &Inst);
64 void EmitInstToData(const MCInst &Inst);
65
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000066public:
Daniel Dunbar1f3e4452010-03-11 01:34:27 +000067 MCMachOStreamer(MCContext &Context, TargetAsmBackend &TAB,
68 raw_ostream &_OS, MCCodeEmitter *_Emitter)
Daniel Dunbarcf871e52010-03-19 10:43:18 +000069 : MCStreamer(Context), Assembler(Context, TAB, *_Emitter, _OS),
Daniel Dunbar4fac7492009-08-27 08:17:51 +000070 CurSectionData(0) {}
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000071 ~MCMachOStreamer() {}
72
Daniel Dunbarac2884a2010-03-25 22:49:09 +000073 MCAssembler &getAssembler() { return Assembler; }
74
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +000075 const MCExpr *AddValueSymbols(const MCExpr *Value) {
76 switch (Value->getKind()) {
Chris Lattner5d917a82010-02-08 19:41:07 +000077 case MCExpr::Target: assert(0 && "Can't handle target exprs yet!");
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +000078 case MCExpr::Constant:
79 break;
80
81 case MCExpr::Binary: {
82 const MCBinaryExpr *BE = cast<MCBinaryExpr>(Value);
83 AddValueSymbols(BE->getLHS());
84 AddValueSymbols(BE->getRHS());
85 break;
86 }
87
88 case MCExpr::SymbolRef:
Daniel Dunbar46836a72010-03-10 20:58:29 +000089 Assembler.getOrCreateSymbolData(
90 cast<MCSymbolRefExpr>(Value)->getSymbol());
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +000091 break;
92
93 case MCExpr::Unary:
94 AddValueSymbols(cast<MCUnaryExpr>(Value)->getSubExpr());
95 break;
96 }
97
98 return Value;
99 }
100
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000101 /// @name MCStreamer Interface
102 /// @{
103
104 virtual void SwitchSection(const MCSection *Section);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000105 virtual void EmitLabel(MCSymbol *Symbol);
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000106 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag);
Daniel Dunbar821e3332009-08-31 08:09:28 +0000107 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value);
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000108 virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000109 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
Chris Lattner9eb158d2010-01-23 07:47:02 +0000110 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000111 unsigned ByteAlignment);
Chris Lattnerb54b9dd2010-05-08 19:54:22 +0000112 virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol) {
113 assert(0 && "macho doesn't support this directive");
114 }
115 virtual void EmitCOFFSymbolStorageClass(int StorageClass) {
116 assert(0 && "macho doesn't support this directive");
117 }
118 virtual void EmitCOFFSymbolType(int Type) {
119 assert(0 && "macho doesn't support this directive");
120 }
121 virtual void EndCOFFSymbolDef() {
122 assert(0 && "macho doesn't support this directive");
123 }
Chris Lattner99328ad2010-01-25 07:52:13 +0000124 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
125 assert(0 && "macho doesn't support this directive");
126 }
Chris Lattner9eb158d2010-01-23 07:47:02 +0000127 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size) {
128 assert(0 && "macho doesn't support this directive");
129 }
Daniel Dunbar8751b942009-08-28 05:48:22 +0000130 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000131 unsigned Size = 0, unsigned ByteAlignment = 0);
Eric Christopher011c3f12010-05-18 21:26:41 +0000132 virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
133 uint64_t Size, unsigned ByteAlignment = 0);
Chris Lattneraaec2052010-01-19 19:46:13 +0000134 virtual void EmitBytes(StringRef Data, unsigned AddrSpace);
Chris Lattneraaec2052010-01-19 19:46:13 +0000135 virtual void EmitValue(const MCExpr *Value, unsigned Size,unsigned AddrSpace);
Chris Lattner718fb592010-01-25 21:28:50 +0000136 virtual void EmitGPRel32Value(const MCExpr *Value) {
137 assert(0 && "macho doesn't support this directive");
138 }
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000139 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
140 unsigned ValueSize = 1,
141 unsigned MaxBytesToEmit = 0);
Kevin Enderby6e720482010-02-23 18:26:34 +0000142 virtual void EmitCodeAlignment(unsigned ByteAlignment,
143 unsigned MaxBytesToEmit = 0);
Daniel Dunbar821e3332009-08-31 08:09:28 +0000144 virtual void EmitValueToOffset(const MCExpr *Offset,
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000145 unsigned char Value = 0);
Daniel Dunbar2ac0c452010-05-26 20:37:00 +0000146
Chris Lattnera6594fc2010-01-25 18:58:59 +0000147 virtual void EmitFileDirective(StringRef Filename) {
Daniel Dunbarca1212d2010-05-18 17:28:17 +0000148 report_fatal_error("unsupported directive: '.file'");
Chris Lattnera6594fc2010-01-25 18:58:59 +0000149 }
150 virtual void EmitDwarfFileDirective(unsigned FileNo, StringRef Filename) {
Daniel Dunbarca1212d2010-05-18 17:28:17 +0000151 report_fatal_error("unsupported directive: '.file'");
Chris Lattnera6594fc2010-01-25 18:58:59 +0000152 }
Daniel Dunbar2ac0c452010-05-26 20:37:00 +0000153
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000154 virtual void EmitInstruction(const MCInst &Inst);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000155 virtual void Finish();
156
157 /// @}
158};
159
160} // end anonymous namespace.
161
162void MCMachOStreamer::SwitchSection(const MCSection *Section) {
163 assert(Section && "Cannot switch to a null section!");
Daniel Dunbar2ac0c452010-05-26 20:37:00 +0000164
Chris Lattnercda7f782009-08-22 19:35:08 +0000165 // If already in this section, then this is a noop.
166 if (Section == CurSection) return;
Daniel Dunbar0f5fa692009-08-28 05:48:54 +0000167
Chris Lattnercda7f782009-08-22 19:35:08 +0000168 CurSection = Section;
Daniel Dunbar46836a72010-03-10 20:58:29 +0000169 CurSectionData = &Assembler.getOrCreateSectionData(*Section);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000170}
171
172void MCMachOStreamer::EmitLabel(MCSymbol *Symbol) {
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000173 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
Daniel Dunbarc3047182010-05-05 19:01:00 +0000174 assert(!Symbol->isVariable() && "Cannot emit a variable symbol!");
175 assert(CurSection && "Cannot emit before setting section!");
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000176
Daniel Dunbar071f73d2010-05-10 22:45:09 +0000177 MCSymbolData &SD = Assembler.getOrCreateSymbolData(*Symbol);
178
179 // Update the current atom map, if necessary.
180 bool MustCreateFragment = false;
181 if (Assembler.isSymbolLinkerVisible(&SD)) {
182 CurrentAtomMap[CurSectionData] = &SD;
183
184 // We have to create a new fragment, fragments cannot span atoms.
185 MustCreateFragment = true;
186 }
187
Daniel Dunbarf70f4772010-03-22 20:35:46 +0000188 // FIXME: This is wasteful, we don't necessarily need to create a data
189 // fragment. Instead, we should mark the symbol as pointing into the data
190 // fragment if it exists, otherwise we should just queue the label and set its
191 // fragment pointer when we emit the next fragment.
Daniel Dunbar071f73d2010-05-10 22:45:09 +0000192 MCDataFragment *F =
193 MustCreateFragment ? createDataFragment() : getOrCreateDataFragment();
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000194 assert(!SD.getFragment() && "Unexpected fragment on symbol data!");
195 SD.setFragment(F);
196 SD.setOffset(F->getContents().size());
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000197
Daniel Dunbarb18d2dd2010-05-17 20:12:31 +0000198 // This causes the reference type flag to be cleared. Darwin 'as' was "trying"
199 // to clear the weak reference and weak definition bits too, but the
200 // implementation was buggy. For now we just try to match 'as', for
201 // diffability.
202 //
203 // FIXME: Cleanup this code, these bits should be emitted based on semantic
204 // properties, not on the order of definition, etc.
205 SD.setFlags(SD.getFlags() & ~SF_ReferenceTypeMask);
Daniel Dunbar2ac0c452010-05-26 20:37:00 +0000206
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000207 Symbol->setSection(*CurSection);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000208}
209
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000210void MCMachOStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
Daniel Dunbar6009db42009-08-26 21:22:22 +0000211 switch (Flag) {
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000212 case MCAF_SubsectionsViaSymbols:
Daniel Dunbar6009db42009-08-26 21:22:22 +0000213 Assembler.setSubsectionsViaSymbols(true);
Daniel Dunbar8c3eaf42009-08-28 07:08:47 +0000214 return;
Daniel Dunbar6009db42009-08-26 21:22:22 +0000215 }
Daniel Dunbar8c3eaf42009-08-28 07:08:47 +0000216
217 assert(0 && "invalid assembler flag!");
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000218}
219
Daniel Dunbar821e3332009-08-31 08:09:28 +0000220void MCMachOStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
Daniel Dunbara4d86672009-10-16 01:58:23 +0000221 // FIXME: Lift context changes into super class.
Daniel Dunbar2ae4bfd2010-05-18 17:28:24 +0000222 Assembler.getOrCreateSymbolData(*Symbol);
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000223 Symbol->setVariableValue(AddValueSymbols(Value));
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000224}
225
226void MCMachOStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000227 MCSymbolAttr Attribute) {
Daniel Dunbar0c7761b2009-08-24 11:56:58 +0000228 // Indirect symbols are handled differently, to match how 'as' handles
229 // them. This makes writing matching .o files easier.
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000230 if (Attribute == MCSA_IndirectSymbol) {
Daniel Dunbarad7c3d52009-08-26 00:18:21 +0000231 // Note that we intentionally cannot use the symbol data here; this is
232 // important for matching the string table that 'as' generates.
Daniel Dunbar0c7761b2009-08-24 11:56:58 +0000233 IndirectSymbolData ISD;
234 ISD.Symbol = Symbol;
235 ISD.SectionData = CurSectionData;
236 Assembler.getIndirectSymbols().push_back(ISD);
237 return;
238 }
239
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000240 // Adding a symbol attribute always introduces the symbol, note that an
Daniel Dunbar46836a72010-03-10 20:58:29 +0000241 // important side effect of calling getOrCreateSymbolData here is to register
242 // the symbol with the assembler.
243 MCSymbolData &SD = Assembler.getOrCreateSymbolData(*Symbol);
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000244
245 // The implementation of symbol attributes is designed to match 'as', but it
246 // leaves much to desired. It doesn't really make sense to arbitrarily add and
247 // remove flags, but 'as' allows this (in particular, see .desc).
248 //
249 // In the future it might be worth trying to make these operations more well
250 // defined.
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000251 switch (Attribute) {
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000252 case MCSA_Invalid:
Chris Lattnered0ab152010-01-25 18:30:45 +0000253 case MCSA_ELF_TypeFunction:
254 case MCSA_ELF_TypeIndFunction:
255 case MCSA_ELF_TypeObject:
256 case MCSA_ELF_TypeTLS:
257 case MCSA_ELF_TypeCommon:
258 case MCSA_ELF_TypeNoType:
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000259 case MCSA_IndirectSymbol:
260 case MCSA_Hidden:
261 case MCSA_Internal:
262 case MCSA_Protected:
263 case MCSA_Weak:
264 case MCSA_Local:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000265 assert(0 && "Invalid symbol attribute for Mach-O!");
266 break;
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000267
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000268 case MCSA_Global:
Daniel Dunbar8f4d1462009-08-28 07:08:35 +0000269 SD.setExternal(true);
Daniel Dunbarb18d2dd2010-05-17 20:12:31 +0000270 // This effectively clears the undefined lazy bit, in Darwin 'as', although
271 // it isn't very consistent because it implements this as part of symbol
272 // lookup.
273 //
274 // FIXME: Cleanup this code, these bits should be emitted based on semantic
275 // properties, not on the order of definition, etc.
276 SD.setFlags(SD.getFlags() & ~SF_ReferenceTypeUndefinedLazy);
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000277 break;
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000278
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000279 case MCSA_LazyReference:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000280 // FIXME: This requires -dynamic.
281 SD.setFlags(SD.getFlags() | SF_NoDeadStrip);
282 if (Symbol->isUndefined())
283 SD.setFlags(SD.getFlags() | SF_ReferenceTypeUndefinedLazy);
284 break;
285
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000286 // Since .reference sets the no dead strip bit, it is equivalent to
287 // .no_dead_strip in practice.
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000288 case MCSA_Reference:
289 case MCSA_NoDeadStrip:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000290 SD.setFlags(SD.getFlags() | SF_NoDeadStrip);
291 break;
292
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000293 case MCSA_PrivateExtern:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000294 SD.setExternal(true);
295 SD.setPrivateExtern(true);
296 break;
297
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000298 case MCSA_WeakReference:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000299 // FIXME: This requires -dynamic.
300 if (Symbol->isUndefined())
301 SD.setFlags(SD.getFlags() | SF_WeakReference);
302 break;
303
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000304 case MCSA_WeakDefinition:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000305 // FIXME: 'as' enforces that this is defined and global. The manual claims
306 // it has to be in a coalesced section, but this isn't enforced.
307 SD.setFlags(SD.getFlags() | SF_WeakDefinition);
308 break;
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000309 }
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000310}
311
312void MCMachOStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000313 // Encode the 'desc' value into the lowest implementation defined bits.
Daniel Dunbar2ac0c452010-05-26 20:37:00 +0000314 assert(DescValue == (DescValue & SF_DescFlagsMask) &&
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000315 "Invalid .desc value!");
Daniel Dunbar46836a72010-03-10 20:58:29 +0000316 Assembler.getOrCreateSymbolData(*Symbol).setFlags(DescValue&SF_DescFlagsMask);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000317}
318
Chris Lattner9eb158d2010-01-23 07:47:02 +0000319void MCMachOStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000320 unsigned ByteAlignment) {
Daniel Dunbar8f4d1462009-08-28 07:08:35 +0000321 // FIXME: Darwin 'as' does appear to allow redef of a .comm by itself.
322 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
323
Daniel Dunbar46836a72010-03-10 20:58:29 +0000324 MCSymbolData &SD = Assembler.getOrCreateSymbolData(*Symbol);
Daniel Dunbar8f4d1462009-08-28 07:08:35 +0000325 SD.setExternal(true);
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000326 SD.setCommon(Size, ByteAlignment);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000327}
328
Daniel Dunbar8751b942009-08-28 05:48:22 +0000329void MCMachOStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000330 unsigned Size, unsigned ByteAlignment) {
Daniel Dunbar46836a72010-03-10 20:58:29 +0000331 MCSectionData &SectData = Assembler.getOrCreateSectionData(*Section);
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000332
333 // The symbol may not be present, which only creates the section.
334 if (!Symbol)
335 return;
336
337 // FIXME: Assert that this section has the zerofill type.
338
339 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
340
Daniel Dunbar46836a72010-03-10 20:58:29 +0000341 MCSymbolData &SD = Assembler.getOrCreateSymbolData(*Symbol);
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000342
Daniel Dunbare73d49e2010-05-12 22:51:27 +0000343 // Emit an align fragment if necessary.
344 if (ByteAlignment != 1)
Daniel Dunbar1c154132010-05-12 22:56:23 +0000345 new MCAlignFragment(ByteAlignment, 0, 0, ByteAlignment, &SectData);
Daniel Dunbare73d49e2010-05-12 22:51:27 +0000346
Daniel Dunbar4e544872010-05-12 22:51:38 +0000347 MCFragment *F = new MCFillFragment(0, 0, Size, &SectData);
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000348 SD.setFragment(F);
Daniel Dunbar071f73d2010-05-10 22:45:09 +0000349 if (Assembler.isSymbolLinkerVisible(&SD))
350 F->setAtom(&SD);
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000351
352 Symbol->setSection(*Section);
353
354 // Update the maximum alignment on the zero fill section if necessary.
355 if (ByteAlignment > SectData.getAlignment())
356 SectData.setAlignment(ByteAlignment);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000357}
358
Eric Christopher070c1ab2010-05-21 23:03:53 +0000359// This should always be called with the thread local bss section. Like the
360// .zerofill directive this doesn't actually switch sections on us.
Eric Christopher011c3f12010-05-18 21:26:41 +0000361void MCMachOStreamer::EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
362 uint64_t Size, unsigned ByteAlignment) {
363 EmitZerofill(Section, Symbol, Size, ByteAlignment);
364 return;
Eric Christopher482eba02010-05-14 01:50:28 +0000365}
366
Chris Lattneraaec2052010-01-19 19:46:13 +0000367void MCMachOStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {
Daniel Dunbarf70f4772010-03-22 20:35:46 +0000368 getOrCreateDataFragment()->getContents().append(Data.begin(), Data.end());
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000369}
370
Chris Lattneraaec2052010-01-19 19:46:13 +0000371void MCMachOStreamer::EmitValue(const MCExpr *Value, unsigned Size,
372 unsigned AddrSpace) {
Daniel Dunbarf70f4772010-03-22 20:35:46 +0000373 MCDataFragment *DF = getOrCreateDataFragment();
Daniel Dunbar45f48742010-02-13 09:28:22 +0000374
375 // Avoid fixups when possible.
376 int64_t AbsValue;
Daniel Dunbar40ebe2472010-02-22 22:08:57 +0000377 if (AddValueSymbols(Value)->EvaluateAsAbsolute(AbsValue)) {
Daniel Dunbar45f48742010-02-13 09:28:22 +0000378 // FIXME: Endianness assumption.
379 for (unsigned i = 0; i != Size; ++i)
380 DF->getContents().push_back(uint8_t(AbsValue >> (i * 8)));
381 } else {
Daniel Dunbarc90e30a2010-05-26 15:18:56 +0000382 DF->addFixup(MCFixup::Create(DF->getContents().size(),
383 AddValueSymbols(Value),
384 MCFixup::getKindForSize(Size)));
Daniel Dunbar45f48742010-02-13 09:28:22 +0000385 DF->getContents().resize(DF->getContents().size() + Size, 0);
386 }
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000387}
388
389void MCMachOStreamer::EmitValueToAlignment(unsigned ByteAlignment,
390 int64_t Value, unsigned ValueSize,
391 unsigned MaxBytesToEmit) {
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000392 if (MaxBytesToEmit == 0)
393 MaxBytesToEmit = ByteAlignment;
Daniel Dunbar071f73d2010-05-10 22:45:09 +0000394 MCFragment *F = new MCAlignFragment(ByteAlignment, Value, ValueSize,
Daniel Dunbar1c154132010-05-12 22:56:23 +0000395 MaxBytesToEmit, CurSectionData);
Daniel Dunbar071f73d2010-05-10 22:45:09 +0000396 F->setAtom(CurrentAtomMap.lookup(CurSectionData));
Kevin Enderby6e720482010-02-23 18:26:34 +0000397
398 // Update the maximum alignment on the current section if necessary.
399 if (ByteAlignment > CurSectionData->getAlignment())
400 CurSectionData->setAlignment(ByteAlignment);
401}
402
403void MCMachOStreamer::EmitCodeAlignment(unsigned ByteAlignment,
404 unsigned MaxBytesToEmit) {
405 if (MaxBytesToEmit == 0)
406 MaxBytesToEmit = ByteAlignment;
Daniel Dunbar1c154132010-05-12 22:56:23 +0000407 MCAlignFragment *F = new MCAlignFragment(ByteAlignment, 0, 1, MaxBytesToEmit,
408 CurSectionData);
409 F->setEmitNops(true);
Daniel Dunbar071f73d2010-05-10 22:45:09 +0000410 F->setAtom(CurrentAtomMap.lookup(CurSectionData));
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000411
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000412 // Update the maximum alignment on the current section if necessary.
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000413 if (ByteAlignment > CurSectionData->getAlignment())
414 CurSectionData->setAlignment(ByteAlignment);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000415}
416
Daniel Dunbar821e3332009-08-31 08:09:28 +0000417void MCMachOStreamer::EmitValueToOffset(const MCExpr *Offset,
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000418 unsigned char Value) {
Daniel Dunbar071f73d2010-05-10 22:45:09 +0000419 MCFragment *F = new MCOrgFragment(*Offset, Value, CurSectionData);
420 F->setAtom(CurrentAtomMap.lookup(CurSectionData));
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000421}
422
Daniel Dunbar2ac0c452010-05-26 20:37:00 +0000423void MCMachOStreamer::EmitInstToFragment(const MCInst &Inst) {
424 MCInstFragment *IF = new MCInstFragment(Inst, CurSectionData);
425 IF->setAtom(CurrentAtomMap.lookup(CurSectionData));
426
427 // Add the fixups and data.
428 //
429 // FIXME: Revisit this design decision when relaxation is done, we may be
430 // able to get away with not storing any extra data in the MCInst.
431 SmallVector<MCFixup, 4> Fixups;
432 SmallString<256> Code;
433 raw_svector_ostream VecOS(Code);
434 Assembler.getEmitter().EncodeInstruction(Inst, VecOS, Fixups);
435 VecOS.flush();
436
437 IF->getCode() = Code;
438 IF->getFixups() = Fixups;
439}
440
441void MCMachOStreamer::EmitInstToData(const MCInst &Inst) {
442 MCDataFragment *DF = getOrCreateDataFragment();
443
444 SmallVector<MCFixup, 4> Fixups;
445 SmallString<256> Code;
446 raw_svector_ostream VecOS(Code);
447 Assembler.getEmitter().EncodeInstruction(Inst, VecOS, Fixups);
448 VecOS.flush();
449
450 // Add the fixups and data.
451 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
452 Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size());
453 DF->addFixup(Fixups[i]);
454 }
455 DF->getContents().append(Code.begin(), Code.end());
456}
457
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000458void MCMachOStreamer::EmitInstruction(const MCInst &Inst) {
Daniel Dunbar4fac7492009-08-27 08:17:51 +0000459 // Scan for values.
Daniel Dunbardb9014d2010-05-17 21:19:59 +0000460 for (unsigned i = Inst.getNumOperands(); i--; )
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000461 if (Inst.getOperand(i).isExpr())
462 AddValueSymbols(Inst.getOperand(i).getExpr());
Daniel Dunbar4fac7492009-08-27 08:17:51 +0000463
Daniel Dunbare1ec6172010-02-02 21:44:01 +0000464 CurSectionData->setHasInstructions(true);
465
Daniel Dunbar83194de2010-05-26 20:37:03 +0000466 // If this instruction doesn't need relaxation, just emit it as data.
467 if (!Assembler.getBackend().MayNeedRelaxation(Inst)) {
Daniel Dunbar2ac0c452010-05-26 20:37:00 +0000468 EmitInstToData(Inst);
Daniel Dunbar83194de2010-05-26 20:37:03 +0000469 return;
470 }
471
472 // Otherwise, if we are relaxing everything, relax the instruction as much as
473 // possible and emit it as data.
474 if (Assembler.getRelaxAll()) {
475 MCInst Relaxed;
476 Assembler.getBackend().RelaxInstruction(Inst, Relaxed);
477 while (Assembler.getBackend().MayNeedRelaxation(Relaxed))
478 Assembler.getBackend().RelaxInstruction(Relaxed, Relaxed);
479 EmitInstToData(Relaxed);
480 return;
481 }
482
483 // Otherwise emit to a separate fragment.
484 EmitInstToFragment(Inst);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000485}
486
487void MCMachOStreamer::Finish() {
488 Assembler.Finish();
489}
490
Daniel Dunbar1f3e4452010-03-11 01:34:27 +0000491MCStreamer *llvm::createMachOStreamer(MCContext &Context, TargetAsmBackend &TAB,
Daniel Dunbarac2884a2010-03-25 22:49:09 +0000492 raw_ostream &OS, MCCodeEmitter *CE,
493 bool RelaxAll) {
494 MCMachOStreamer *S = new MCMachOStreamer(Context, TAB, OS, CE);
495 if (RelaxAll)
496 S->getAssembler().setRelaxAll(true);
497 return S;
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000498}