blob: fc394b5f60c4228d4987a73631660dd23dd29099 [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 Dunbar2ac0c452010-05-26 20:37:00 +000031 void EmitInstToFragment(const MCInst &Inst);
32 void EmitInstToData(const MCInst &Inst);
33
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000034public:
Daniel Dunbar1f3e4452010-03-11 01:34:27 +000035 MCMachOStreamer(MCContext &Context, TargetAsmBackend &TAB,
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +000036 raw_ostream &OS, MCCodeEmitter *Emitter)
Daniel Dunbar83b46712010-06-16 20:04:25 +000037 : MCObjectStreamer(Context, TAB, OS, Emitter) {}
Daniel Dunbarac2884a2010-03-25 22:49:09 +000038
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000039 /// @name MCStreamer Interface
40 /// @{
41
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000042 virtual void EmitLabel(MCSymbol *Symbol);
Chris Lattnera5ad93a2010-01-23 06:39:22 +000043 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag);
Daniel Dunbar821e3332009-08-31 08:09:28 +000044 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value);
Chris Lattnera5ad93a2010-01-23 06:39:22 +000045 virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000046 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
Chris Lattner9eb158d2010-01-23 07:47:02 +000047 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +000048 unsigned ByteAlignment);
Chris Lattnerb54b9dd2010-05-08 19:54:22 +000049 virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol) {
50 assert(0 && "macho doesn't support this directive");
51 }
52 virtual void EmitCOFFSymbolStorageClass(int StorageClass) {
53 assert(0 && "macho doesn't support this directive");
54 }
55 virtual void EmitCOFFSymbolType(int Type) {
56 assert(0 && "macho doesn't support this directive");
57 }
58 virtual void EndCOFFSymbolDef() {
59 assert(0 && "macho doesn't support this directive");
60 }
Chris Lattner99328ad2010-01-25 07:52:13 +000061 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
62 assert(0 && "macho doesn't support this directive");
63 }
Chris Lattner9eb158d2010-01-23 07:47:02 +000064 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size) {
65 assert(0 && "macho doesn't support this directive");
66 }
Daniel Dunbar8751b942009-08-28 05:48:22 +000067 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +000068 unsigned Size = 0, unsigned ByteAlignment = 0);
Eric Christopher011c3f12010-05-18 21:26:41 +000069 virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
70 uint64_t Size, unsigned ByteAlignment = 0);
Chris Lattneraaec2052010-01-19 19:46:13 +000071 virtual void EmitBytes(StringRef Data, unsigned AddrSpace);
Chris Lattneraaec2052010-01-19 19:46:13 +000072 virtual void EmitValue(const MCExpr *Value, unsigned Size,unsigned AddrSpace);
Chris Lattner718fb592010-01-25 21:28:50 +000073 virtual void EmitGPRel32Value(const MCExpr *Value) {
74 assert(0 && "macho doesn't support this directive");
75 }
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000076 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
77 unsigned ValueSize = 1,
78 unsigned MaxBytesToEmit = 0);
Kevin Enderby6e720482010-02-23 18:26:34 +000079 virtual void EmitCodeAlignment(unsigned ByteAlignment,
80 unsigned MaxBytesToEmit = 0);
Daniel Dunbar821e3332009-08-31 08:09:28 +000081 virtual void EmitValueToOffset(const MCExpr *Offset,
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000082 unsigned char Value = 0);
Daniel Dunbar2ac0c452010-05-26 20:37:00 +000083
Chris Lattnera6594fc2010-01-25 18:58:59 +000084 virtual void EmitFileDirective(StringRef Filename) {
Daniel Dunbar96749672010-07-19 20:44:20 +000085 // FIXME: Just ignore the .file; it isn't important enough to fail the
86 // entire assembly.
87
88 //report_fatal_error("unsupported directive: '.file'");
Chris Lattnera6594fc2010-01-25 18:58:59 +000089 }
90 virtual void EmitDwarfFileDirective(unsigned FileNo, StringRef Filename) {
Daniel Dunbar96749672010-07-19 20:44:20 +000091 // FIXME: Just ignore the .file; it isn't important enough to fail the
92 // entire assembly.
93
94 //report_fatal_error("unsupported directive: '.file'");
Chris Lattnera6594fc2010-01-25 18:58:59 +000095 }
Daniel Dunbar2ac0c452010-05-26 20:37:00 +000096
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000097 virtual void EmitInstruction(const MCInst &Inst);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000098
Daniel Dunbara86de102010-06-16 20:04:32 +000099 virtual void Finish();
100
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000101 /// @}
102};
103
104} // end anonymous namespace.
105
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000106void MCMachOStreamer::EmitLabel(MCSymbol *Symbol) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000107 // TODO: This is almost exactly the same as WinCOFFStreamer. Consider merging
108 // into MCObjectStreamer.
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000109 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
Daniel Dunbarc3047182010-05-05 19:01:00 +0000110 assert(!Symbol->isVariable() && "Cannot emit a variable symbol!");
111 assert(CurSection && "Cannot emit before setting section!");
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000112
Daniel Dunbara86de102010-06-16 20:04:32 +0000113 Symbol->setSection(*CurSection);
114
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000115 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
Daniel Dunbar071f73d2010-05-10 22:45:09 +0000116
Daniel Dunbara86de102010-06-16 20:04:32 +0000117 // We have to create a new fragment if this is an atom defining symbol,
118 // fragments cannot span atoms.
119 if (getAssembler().isSymbolLinkerVisible(SD.getSymbol()))
120 new MCDataFragment(getCurrentSectionData());
Daniel Dunbar071f73d2010-05-10 22:45:09 +0000121
Daniel Dunbarf70f4772010-03-22 20:35:46 +0000122 // FIXME: This is wasteful, we don't necessarily need to create a data
123 // fragment. Instead, we should mark the symbol as pointing into the data
124 // fragment if it exists, otherwise we should just queue the label and set its
125 // fragment pointer when we emit the next fragment.
Daniel Dunbara86de102010-06-16 20:04:32 +0000126 MCDataFragment *F = getOrCreateDataFragment();
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000127 assert(!SD.getFragment() && "Unexpected fragment on symbol data!");
128 SD.setFragment(F);
129 SD.setOffset(F->getContents().size());
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000130
Daniel Dunbarb18d2dd2010-05-17 20:12:31 +0000131 // This causes the reference type flag to be cleared. Darwin 'as' was "trying"
132 // to clear the weak reference and weak definition bits too, but the
133 // implementation was buggy. For now we just try to match 'as', for
134 // diffability.
135 //
136 // FIXME: Cleanup this code, these bits should be emitted based on semantic
137 // properties, not on the order of definition, etc.
138 SD.setFlags(SD.getFlags() & ~SF_ReferenceTypeMask);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000139}
140
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000141void MCMachOStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
Daniel Dunbar6009db42009-08-26 21:22:22 +0000142 switch (Flag) {
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000143 case MCAF_SubsectionsViaSymbols:
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000144 getAssembler().setSubsectionsViaSymbols(true);
Daniel Dunbar8c3eaf42009-08-28 07:08:47 +0000145 return;
Daniel Dunbar6009db42009-08-26 21:22:22 +0000146 }
Daniel Dunbar8c3eaf42009-08-28 07:08:47 +0000147
148 assert(0 && "invalid assembler flag!");
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000149}
150
Daniel Dunbar821e3332009-08-31 08:09:28 +0000151void MCMachOStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000152 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
153 // MCObjectStreamer.
Daniel Dunbara4d86672009-10-16 01:58:23 +0000154 // FIXME: Lift context changes into super class.
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000155 getAssembler().getOrCreateSymbolData(*Symbol);
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000156 Symbol->setVariableValue(AddValueSymbols(Value));
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000157}
158
159void MCMachOStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000160 MCSymbolAttr Attribute) {
Daniel Dunbar0c7761b2009-08-24 11:56:58 +0000161 // Indirect symbols are handled differently, to match how 'as' handles
162 // them. This makes writing matching .o files easier.
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000163 if (Attribute == MCSA_IndirectSymbol) {
Daniel Dunbarad7c3d52009-08-26 00:18:21 +0000164 // Note that we intentionally cannot use the symbol data here; this is
165 // important for matching the string table that 'as' generates.
Daniel Dunbar0c7761b2009-08-24 11:56:58 +0000166 IndirectSymbolData ISD;
167 ISD.Symbol = Symbol;
Daniel Dunbar83b46712010-06-16 20:04:25 +0000168 ISD.SectionData = getCurrentSectionData();
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000169 getAssembler().getIndirectSymbols().push_back(ISD);
Daniel Dunbar0c7761b2009-08-24 11:56:58 +0000170 return;
171 }
172
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000173 // Adding a symbol attribute always introduces the symbol, note that an
Daniel Dunbar46836a72010-03-10 20:58:29 +0000174 // important side effect of calling getOrCreateSymbolData here is to register
175 // the symbol with the assembler.
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000176 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000177
178 // The implementation of symbol attributes is designed to match 'as', but it
179 // leaves much to desired. It doesn't really make sense to arbitrarily add and
180 // remove flags, but 'as' allows this (in particular, see .desc).
181 //
182 // In the future it might be worth trying to make these operations more well
183 // defined.
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000184 switch (Attribute) {
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000185 case MCSA_Invalid:
Chris Lattnered0ab152010-01-25 18:30:45 +0000186 case MCSA_ELF_TypeFunction:
187 case MCSA_ELF_TypeIndFunction:
188 case MCSA_ELF_TypeObject:
189 case MCSA_ELF_TypeTLS:
190 case MCSA_ELF_TypeCommon:
191 case MCSA_ELF_TypeNoType:
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000192 case MCSA_IndirectSymbol:
193 case MCSA_Hidden:
194 case MCSA_Internal:
195 case MCSA_Protected:
196 case MCSA_Weak:
197 case MCSA_Local:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000198 assert(0 && "Invalid symbol attribute for Mach-O!");
199 break;
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000200
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000201 case MCSA_Global:
Daniel Dunbar8f4d1462009-08-28 07:08:35 +0000202 SD.setExternal(true);
Daniel Dunbarb18d2dd2010-05-17 20:12:31 +0000203 // This effectively clears the undefined lazy bit, in Darwin 'as', although
204 // it isn't very consistent because it implements this as part of symbol
205 // lookup.
206 //
207 // FIXME: Cleanup this code, these bits should be emitted based on semantic
208 // properties, not on the order of definition, etc.
209 SD.setFlags(SD.getFlags() & ~SF_ReferenceTypeUndefinedLazy);
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000210 break;
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000211
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000212 case MCSA_LazyReference:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000213 // FIXME: This requires -dynamic.
214 SD.setFlags(SD.getFlags() | SF_NoDeadStrip);
215 if (Symbol->isUndefined())
216 SD.setFlags(SD.getFlags() | SF_ReferenceTypeUndefinedLazy);
217 break;
218
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000219 // Since .reference sets the no dead strip bit, it is equivalent to
220 // .no_dead_strip in practice.
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000221 case MCSA_Reference:
222 case MCSA_NoDeadStrip:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000223 SD.setFlags(SD.getFlags() | SF_NoDeadStrip);
224 break;
225
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000226 case MCSA_PrivateExtern:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000227 SD.setExternal(true);
228 SD.setPrivateExtern(true);
229 break;
230
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000231 case MCSA_WeakReference:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000232 // FIXME: This requires -dynamic.
233 if (Symbol->isUndefined())
234 SD.setFlags(SD.getFlags() | SF_WeakReference);
235 break;
236
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000237 case MCSA_WeakDefinition:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000238 // FIXME: 'as' enforces that this is defined and global. The manual claims
239 // it has to be in a coalesced section, but this isn't enforced.
240 SD.setFlags(SD.getFlags() | SF_WeakDefinition);
241 break;
Kevin Enderbyf59cac52010-07-08 17:22:42 +0000242
243 case MCSA_WeakDefAutoPrivate:
244 SD.setFlags(SD.getFlags() | SF_WeakDefinition | SF_WeakReference);
245 break;
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000246 }
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000247}
248
249void MCMachOStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000250 // Encode the 'desc' value into the lowest implementation defined bits.
Daniel Dunbar2ac0c452010-05-26 20:37:00 +0000251 assert(DescValue == (DescValue & SF_DescFlagsMask) &&
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000252 "Invalid .desc value!");
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000253 getAssembler().getOrCreateSymbolData(*Symbol).setFlags(
254 DescValue & SF_DescFlagsMask);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000255}
256
Chris Lattner9eb158d2010-01-23 07:47:02 +0000257void MCMachOStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000258 unsigned ByteAlignment) {
Daniel Dunbar8f4d1462009-08-28 07:08:35 +0000259 // FIXME: Darwin 'as' does appear to allow redef of a .comm by itself.
260 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
261
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000262 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
Daniel Dunbar8f4d1462009-08-28 07:08:35 +0000263 SD.setExternal(true);
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000264 SD.setCommon(Size, ByteAlignment);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000265}
266
Daniel Dunbar8751b942009-08-28 05:48:22 +0000267void MCMachOStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000268 unsigned Size, unsigned ByteAlignment) {
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000269 MCSectionData &SectData = getAssembler().getOrCreateSectionData(*Section);
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000270
271 // The symbol may not be present, which only creates the section.
272 if (!Symbol)
273 return;
274
275 // FIXME: Assert that this section has the zerofill type.
276
277 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
278
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000279 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000280
Daniel Dunbare73d49e2010-05-12 22:51:27 +0000281 // Emit an align fragment if necessary.
282 if (ByteAlignment != 1)
Daniel Dunbar1c154132010-05-12 22:56:23 +0000283 new MCAlignFragment(ByteAlignment, 0, 0, ByteAlignment, &SectData);
Daniel Dunbare73d49e2010-05-12 22:51:27 +0000284
Daniel Dunbar4e544872010-05-12 22:51:38 +0000285 MCFragment *F = new MCFillFragment(0, 0, Size, &SectData);
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000286 SD.setFragment(F);
287
288 Symbol->setSection(*Section);
289
290 // Update the maximum alignment on the zero fill section if necessary.
291 if (ByteAlignment > SectData.getAlignment())
292 SectData.setAlignment(ByteAlignment);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000293}
294
Eric Christopher070c1ab2010-05-21 23:03:53 +0000295// This should always be called with the thread local bss section. Like the
296// .zerofill directive this doesn't actually switch sections on us.
Eric Christopher011c3f12010-05-18 21:26:41 +0000297void MCMachOStreamer::EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
298 uint64_t Size, unsigned ByteAlignment) {
299 EmitZerofill(Section, Symbol, Size, ByteAlignment);
300 return;
Eric Christopher482eba02010-05-14 01:50:28 +0000301}
302
Chris Lattneraaec2052010-01-19 19:46:13 +0000303void MCMachOStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000304 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
305 // MCObjectStreamer.
Daniel Dunbarf70f4772010-03-22 20:35:46 +0000306 getOrCreateDataFragment()->getContents().append(Data.begin(), Data.end());
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000307}
308
Chris Lattneraaec2052010-01-19 19:46:13 +0000309void MCMachOStreamer::EmitValue(const MCExpr *Value, unsigned Size,
310 unsigned AddrSpace) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000311 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
312 // MCObjectStreamer.
Daniel Dunbarf70f4772010-03-22 20:35:46 +0000313 MCDataFragment *DF = getOrCreateDataFragment();
Daniel Dunbar45f48742010-02-13 09:28:22 +0000314
315 // Avoid fixups when possible.
316 int64_t AbsValue;
Daniel Dunbar40ebe2472010-02-22 22:08:57 +0000317 if (AddValueSymbols(Value)->EvaluateAsAbsolute(AbsValue)) {
Daniel Dunbar45f48742010-02-13 09:28:22 +0000318 // FIXME: Endianness assumption.
319 for (unsigned i = 0; i != Size; ++i)
320 DF->getContents().push_back(uint8_t(AbsValue >> (i * 8)));
321 } else {
Daniel Dunbarc90e30a2010-05-26 15:18:56 +0000322 DF->addFixup(MCFixup::Create(DF->getContents().size(),
323 AddValueSymbols(Value),
324 MCFixup::getKindForSize(Size)));
Daniel Dunbar45f48742010-02-13 09:28:22 +0000325 DF->getContents().resize(DF->getContents().size() + Size, 0);
326 }
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000327}
328
329void MCMachOStreamer::EmitValueToAlignment(unsigned ByteAlignment,
330 int64_t Value, unsigned ValueSize,
331 unsigned MaxBytesToEmit) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000332 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
333 // MCObjectStreamer.
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000334 if (MaxBytesToEmit == 0)
335 MaxBytesToEmit = ByteAlignment;
Daniel Dunbara86de102010-06-16 20:04:32 +0000336 new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit,
337 getCurrentSectionData());
Kevin Enderby6e720482010-02-23 18:26:34 +0000338
339 // Update the maximum alignment on the current section if necessary.
Daniel Dunbar83b46712010-06-16 20:04:25 +0000340 if (ByteAlignment > getCurrentSectionData()->getAlignment())
341 getCurrentSectionData()->setAlignment(ByteAlignment);
Kevin Enderby6e720482010-02-23 18:26:34 +0000342}
343
344void MCMachOStreamer::EmitCodeAlignment(unsigned ByteAlignment,
345 unsigned MaxBytesToEmit) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000346 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
347 // MCObjectStreamer.
Kevin Enderby6e720482010-02-23 18:26:34 +0000348 if (MaxBytesToEmit == 0)
349 MaxBytesToEmit = ByteAlignment;
Daniel Dunbar1c154132010-05-12 22:56:23 +0000350 MCAlignFragment *F = new MCAlignFragment(ByteAlignment, 0, 1, MaxBytesToEmit,
Daniel Dunbar83b46712010-06-16 20:04:25 +0000351 getCurrentSectionData());
Daniel Dunbar1c154132010-05-12 22:56:23 +0000352 F->setEmitNops(true);
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 Dunbar83b46712010-06-16 20:04:25 +0000355 if (ByteAlignment > getCurrentSectionData()->getAlignment())
356 getCurrentSectionData()->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 Dunbara86de102010-06-16 20:04:32 +0000361 new MCOrgFragment(*Offset, Value, getCurrentSectionData());
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000362}
363
Daniel Dunbar2ac0c452010-05-26 20:37:00 +0000364void MCMachOStreamer::EmitInstToFragment(const MCInst &Inst) {
Daniel Dunbar83b46712010-06-16 20:04:25 +0000365 MCInstFragment *IF = new MCInstFragment(Inst, getCurrentSectionData());
Daniel Dunbar2ac0c452010-05-26 20:37:00 +0000366
367 // Add the fixups and data.
368 //
369 // FIXME: Revisit this design decision when relaxation is done, we may be
370 // able to get away with not storing any extra data in the MCInst.
371 SmallVector<MCFixup, 4> Fixups;
372 SmallString<256> Code;
373 raw_svector_ostream VecOS(Code);
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000374 getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups);
Daniel Dunbar2ac0c452010-05-26 20:37:00 +0000375 VecOS.flush();
376
377 IF->getCode() = Code;
378 IF->getFixups() = Fixups;
379}
380
381void MCMachOStreamer::EmitInstToData(const MCInst &Inst) {
382 MCDataFragment *DF = getOrCreateDataFragment();
383
384 SmallVector<MCFixup, 4> Fixups;
385 SmallString<256> Code;
386 raw_svector_ostream VecOS(Code);
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000387 getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups);
Daniel Dunbar2ac0c452010-05-26 20:37:00 +0000388 VecOS.flush();
389
390 // Add the fixups and data.
391 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
392 Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size());
393 DF->addFixup(Fixups[i]);
394 }
395 DF->getContents().append(Code.begin(), Code.end());
396}
397
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000398void MCMachOStreamer::EmitInstruction(const MCInst &Inst) {
Daniel Dunbar4fac7492009-08-27 08:17:51 +0000399 // Scan for values.
Daniel Dunbardb9014d2010-05-17 21:19:59 +0000400 for (unsigned i = Inst.getNumOperands(); i--; )
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000401 if (Inst.getOperand(i).isExpr())
402 AddValueSymbols(Inst.getOperand(i).getExpr());
Daniel Dunbar4fac7492009-08-27 08:17:51 +0000403
Daniel Dunbar83b46712010-06-16 20:04:25 +0000404 getCurrentSectionData()->setHasInstructions(true);
Daniel Dunbare1ec6172010-02-02 21:44:01 +0000405
Daniel Dunbar83194de2010-05-26 20:37:03 +0000406 // If this instruction doesn't need relaxation, just emit it as data.
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000407 if (!getAssembler().getBackend().MayNeedRelaxation(Inst)) {
Daniel Dunbar2ac0c452010-05-26 20:37:00 +0000408 EmitInstToData(Inst);
Daniel Dunbar83194de2010-05-26 20:37:03 +0000409 return;
410 }
411
412 // Otherwise, if we are relaxing everything, relax the instruction as much as
413 // possible and emit it as data.
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000414 if (getAssembler().getRelaxAll()) {
Daniel Dunbar83194de2010-05-26 20:37:03 +0000415 MCInst Relaxed;
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000416 getAssembler().getBackend().RelaxInstruction(Inst, Relaxed);
417 while (getAssembler().getBackend().MayNeedRelaxation(Relaxed))
418 getAssembler().getBackend().RelaxInstruction(Relaxed, Relaxed);
Daniel Dunbar83194de2010-05-26 20:37:03 +0000419 EmitInstToData(Relaxed);
420 return;
421 }
422
423 // Otherwise emit to a separate fragment.
424 EmitInstToFragment(Inst);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000425}
426
Daniel Dunbara86de102010-06-16 20:04:32 +0000427void MCMachOStreamer::Finish() {
428 // We have to set the fragment atom associations so we can relax properly for
429 // Mach-O.
430
431 // First, scan the symbol table to build a lookup table from fragments to
432 // defining symbols.
433 DenseMap<const MCFragment*, MCSymbolData*> DefiningSymbolMap;
434 for (MCAssembler::symbol_iterator it = getAssembler().symbol_begin(),
435 ie = getAssembler().symbol_end(); it != ie; ++it) {
436 if (getAssembler().isSymbolLinkerVisible(it->getSymbol()) &&
437 it->getFragment()) {
438 // An atom defining symbol should never be internal to a fragment.
439 assert(it->getOffset() == 0 && "Invalid offset in atom defining symbol!");
440 DefiningSymbolMap[it->getFragment()] = it;
441 }
442 }
443
444 // Set the fragment atom associations by tracking the last seen atom defining
445 // symbol.
446 for (MCAssembler::iterator it = getAssembler().begin(),
447 ie = getAssembler().end(); it != ie; ++it) {
448 MCSymbolData *CurrentAtom = 0;
449 for (MCSectionData::iterator it2 = it->begin(),
450 ie2 = it->end(); it2 != ie2; ++it2) {
451 if (MCSymbolData *SD = DefiningSymbolMap.lookup(it2))
452 CurrentAtom = SD;
453 it2->setAtom(CurrentAtom);
454 }
455 }
456
457 this->MCObjectStreamer::Finish();
458}
459
Daniel Dunbar1f3e4452010-03-11 01:34:27 +0000460MCStreamer *llvm::createMachOStreamer(MCContext &Context, TargetAsmBackend &TAB,
Daniel Dunbarac2884a2010-03-25 22:49:09 +0000461 raw_ostream &OS, MCCodeEmitter *CE,
462 bool RelaxAll) {
463 MCMachOStreamer *S = new MCMachOStreamer(Context, TAB, OS, CE);
464 if (RelaxAll)
465 S->getAssembler().setRelaxAll(true);
466 return S;
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000467}