blob: 21f3002dded1d515378e9f5802cb9abd2af1e8ff [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"
Kevin Enderbyb07ce602010-08-09 22:52:14 +000021#include "llvm/MC/MCSectionMachO.h"
22#include "llvm/MC/MCDwarf.h"
Kevin Enderbyc0957932010-09-30 16:52:03 +000023#include "llvm/Support/Dwarf.h"
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000024#include "llvm/Support/ErrorHandling.h"
Daniel Dunbar4fac7492009-08-27 08:17:51 +000025#include "llvm/Support/raw_ostream.h"
Daniel Dunbard8036fb2010-03-23 05:09:03 +000026#include "llvm/Target/TargetAsmBackend.h"
27
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000028using namespace llvm;
29
30namespace {
31
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +000032class MCMachOStreamer : public MCObjectStreamer {
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +000033private:
Rafael Espindolaf89671d2010-11-01 16:27:31 +000034 virtual void EmitInstToFragment(const MCInst &Inst);
35 virtual void EmitInstToData(const MCInst &Inst);
Daniel Dunbar2ac0c452010-05-26 20:37:00 +000036
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000037public:
Daniel Dunbar1f3e4452010-03-11 01:34:27 +000038 MCMachOStreamer(MCContext &Context, TargetAsmBackend &TAB,
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +000039 raw_ostream &OS, MCCodeEmitter *Emitter)
Rafael Espindola59ff3c92010-09-22 22:27:05 +000040 : MCObjectStreamer(Context, TAB, OS, Emitter, true) {}
Daniel Dunbarac2884a2010-03-25 22:49:09 +000041
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000042 /// @name MCStreamer Interface
43 /// @{
44
Rafael Espindolad80781b2010-09-15 21:48:40 +000045 virtual void InitSections();
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000046 virtual void EmitLabel(MCSymbol *Symbol);
Chris Lattnera5ad93a2010-01-23 06:39:22 +000047 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag);
Daniel Dunbar821e3332009-08-31 08:09:28 +000048 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value);
Chris Lattnera5ad93a2010-01-23 06:39:22 +000049 virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000050 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
Chris Lattner9eb158d2010-01-23 07:47:02 +000051 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +000052 unsigned ByteAlignment);
Chris Lattnerb54b9dd2010-05-08 19:54:22 +000053 virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol) {
54 assert(0 && "macho doesn't support this directive");
55 }
56 virtual void EmitCOFFSymbolStorageClass(int StorageClass) {
57 assert(0 && "macho doesn't support this directive");
58 }
59 virtual void EmitCOFFSymbolType(int Type) {
60 assert(0 && "macho doesn't support this directive");
61 }
62 virtual void EndCOFFSymbolDef() {
63 assert(0 && "macho doesn't support this directive");
64 }
Chris Lattner99328ad2010-01-25 07:52:13 +000065 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
66 assert(0 && "macho doesn't support this directive");
67 }
Chris Lattner9eb158d2010-01-23 07:47:02 +000068 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size) {
69 assert(0 && "macho doesn't support this directive");
70 }
Daniel Dunbar8751b942009-08-28 05:48:22 +000071 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +000072 unsigned Size = 0, unsigned ByteAlignment = 0);
Eric Christopher011c3f12010-05-18 21:26:41 +000073 virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
74 uint64_t Size, unsigned ByteAlignment = 0);
Chris Lattneraaec2052010-01-19 19:46:13 +000075 virtual void EmitBytes(StringRef Data, unsigned AddrSpace);
Chris Lattneraaec2052010-01-19 19:46:13 +000076 virtual void EmitValue(const MCExpr *Value, unsigned Size,unsigned AddrSpace);
Chris Lattner718fb592010-01-25 21:28:50 +000077 virtual void EmitGPRel32Value(const MCExpr *Value) {
78 assert(0 && "macho doesn't support this directive");
79 }
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000080 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
81 unsigned ValueSize = 1,
82 unsigned MaxBytesToEmit = 0);
Kevin Enderby6e720482010-02-23 18:26:34 +000083 virtual void EmitCodeAlignment(unsigned ByteAlignment,
84 unsigned MaxBytesToEmit = 0);
Daniel Dunbar821e3332009-08-31 08:09:28 +000085 virtual void EmitValueToOffset(const MCExpr *Offset,
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000086 unsigned char Value = 0);
Daniel Dunbar2ac0c452010-05-26 20:37:00 +000087
Chris Lattnera6594fc2010-01-25 18:58:59 +000088 virtual void EmitFileDirective(StringRef Filename) {
Daniel Dunbar96749672010-07-19 20:44:20 +000089 // FIXME: Just ignore the .file; it isn't important enough to fail the
90 // entire assembly.
91
92 //report_fatal_error("unsupported directive: '.file'");
Chris Lattnera6594fc2010-01-25 18:58:59 +000093 }
94 virtual void EmitDwarfFileDirective(unsigned FileNo, StringRef Filename) {
Daniel Dunbar96749672010-07-19 20:44:20 +000095 // FIXME: Just ignore the .file; it isn't important enough to fail the
96 // entire assembly.
97
98 //report_fatal_error("unsupported directive: '.file'");
Chris Lattnera6594fc2010-01-25 18:58:59 +000099 }
Daniel Dunbar2ac0c452010-05-26 20:37:00 +0000100
Daniel Dunbara86de102010-06-16 20:04:32 +0000101 virtual void Finish();
102
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000103 /// @}
104};
105
106} // end anonymous namespace.
107
Rafael Espindolad80781b2010-09-15 21:48:40 +0000108void MCMachOStreamer::InitSections() {
109 SwitchSection(getContext().getMachOSection("__TEXT", "__text",
110 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
111 0, SectionKind::getText()));
112
113}
114
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000115void MCMachOStreamer::EmitLabel(MCSymbol *Symbol) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000116 // TODO: This is almost exactly the same as WinCOFFStreamer. Consider merging
117 // into MCObjectStreamer.
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000118 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
Daniel Dunbarc3047182010-05-05 19:01:00 +0000119 assert(!Symbol->isVariable() && "Cannot emit a variable symbol!");
120 assert(CurSection && "Cannot emit before setting section!");
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000121
Daniel Dunbara86de102010-06-16 20:04:32 +0000122 Symbol->setSection(*CurSection);
123
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000124 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
Daniel Dunbar071f73d2010-05-10 22:45:09 +0000125
Daniel Dunbara86de102010-06-16 20:04:32 +0000126 // We have to create a new fragment if this is an atom defining symbol,
127 // fragments cannot span atoms.
128 if (getAssembler().isSymbolLinkerVisible(SD.getSymbol()))
129 new MCDataFragment(getCurrentSectionData());
Daniel Dunbar071f73d2010-05-10 22:45:09 +0000130
Daniel Dunbarf70f4772010-03-22 20:35:46 +0000131 // FIXME: This is wasteful, we don't necessarily need to create a data
132 // fragment. Instead, we should mark the symbol as pointing into the data
133 // fragment if it exists, otherwise we should just queue the label and set its
134 // fragment pointer when we emit the next fragment.
Daniel Dunbara86de102010-06-16 20:04:32 +0000135 MCDataFragment *F = getOrCreateDataFragment();
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000136 assert(!SD.getFragment() && "Unexpected fragment on symbol data!");
137 SD.setFragment(F);
138 SD.setOffset(F->getContents().size());
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000139
Daniel Dunbarb18d2dd2010-05-17 20:12:31 +0000140 // This causes the reference type flag to be cleared. Darwin 'as' was "trying"
141 // to clear the weak reference and weak definition bits too, but the
142 // implementation was buggy. For now we just try to match 'as', for
143 // diffability.
144 //
145 // FIXME: Cleanup this code, these bits should be emitted based on semantic
146 // properties, not on the order of definition, etc.
147 SD.setFlags(SD.getFlags() & ~SF_ReferenceTypeMask);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000148}
149
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000150void MCMachOStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
Daniel Dunbar6009db42009-08-26 21:22:22 +0000151 switch (Flag) {
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000152 case MCAF_SubsectionsViaSymbols:
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000153 getAssembler().setSubsectionsViaSymbols(true);
Daniel Dunbar8c3eaf42009-08-28 07:08:47 +0000154 return;
Chandler Carruth5c0d4722010-09-30 10:59:51 +0000155 default:
156 llvm_unreachable("invalid assembler flag!");
Daniel Dunbar6009db42009-08-26 21:22:22 +0000157 }
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000158}
159
Daniel Dunbar821e3332009-08-31 08:09:28 +0000160void MCMachOStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000161 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
162 // MCObjectStreamer.
Daniel Dunbara4d86672009-10-16 01:58:23 +0000163 // FIXME: Lift context changes into super class.
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000164 getAssembler().getOrCreateSymbolData(*Symbol);
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000165 Symbol->setVariableValue(AddValueSymbols(Value));
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000166}
167
168void MCMachOStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000169 MCSymbolAttr Attribute) {
Daniel Dunbar0c7761b2009-08-24 11:56:58 +0000170 // Indirect symbols are handled differently, to match how 'as' handles
171 // them. This makes writing matching .o files easier.
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000172 if (Attribute == MCSA_IndirectSymbol) {
Daniel Dunbarad7c3d52009-08-26 00:18:21 +0000173 // Note that we intentionally cannot use the symbol data here; this is
174 // important for matching the string table that 'as' generates.
Daniel Dunbar0c7761b2009-08-24 11:56:58 +0000175 IndirectSymbolData ISD;
176 ISD.Symbol = Symbol;
Daniel Dunbar83b46712010-06-16 20:04:25 +0000177 ISD.SectionData = getCurrentSectionData();
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000178 getAssembler().getIndirectSymbols().push_back(ISD);
Daniel Dunbar0c7761b2009-08-24 11:56:58 +0000179 return;
180 }
181
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000182 // Adding a symbol attribute always introduces the symbol, note that an
Daniel Dunbar46836a72010-03-10 20:58:29 +0000183 // important side effect of calling getOrCreateSymbolData here is to register
184 // the symbol with the assembler.
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000185 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000186
187 // The implementation of symbol attributes is designed to match 'as', but it
188 // leaves much to desired. It doesn't really make sense to arbitrarily add and
189 // remove flags, but 'as' allows this (in particular, see .desc).
190 //
191 // In the future it might be worth trying to make these operations more well
192 // defined.
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000193 switch (Attribute) {
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000194 case MCSA_Invalid:
Chris Lattnered0ab152010-01-25 18:30:45 +0000195 case MCSA_ELF_TypeFunction:
196 case MCSA_ELF_TypeIndFunction:
197 case MCSA_ELF_TypeObject:
198 case MCSA_ELF_TypeTLS:
199 case MCSA_ELF_TypeCommon:
200 case MCSA_ELF_TypeNoType:
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000201 case MCSA_IndirectSymbol:
202 case MCSA_Hidden:
203 case MCSA_Internal:
204 case MCSA_Protected:
205 case MCSA_Weak:
206 case MCSA_Local:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000207 assert(0 && "Invalid symbol attribute for Mach-O!");
208 break;
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000209
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000210 case MCSA_Global:
Daniel Dunbar8f4d1462009-08-28 07:08:35 +0000211 SD.setExternal(true);
Daniel Dunbarb18d2dd2010-05-17 20:12:31 +0000212 // This effectively clears the undefined lazy bit, in Darwin 'as', although
213 // it isn't very consistent because it implements this as part of symbol
214 // lookup.
215 //
216 // FIXME: Cleanup this code, these bits should be emitted based on semantic
217 // properties, not on the order of definition, etc.
218 SD.setFlags(SD.getFlags() & ~SF_ReferenceTypeUndefinedLazy);
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000219 break;
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000220
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000221 case MCSA_LazyReference:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000222 // FIXME: This requires -dynamic.
223 SD.setFlags(SD.getFlags() | SF_NoDeadStrip);
224 if (Symbol->isUndefined())
225 SD.setFlags(SD.getFlags() | SF_ReferenceTypeUndefinedLazy);
226 break;
227
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000228 // Since .reference sets the no dead strip bit, it is equivalent to
229 // .no_dead_strip in practice.
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000230 case MCSA_Reference:
231 case MCSA_NoDeadStrip:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000232 SD.setFlags(SD.getFlags() | SF_NoDeadStrip);
233 break;
234
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000235 case MCSA_PrivateExtern:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000236 SD.setExternal(true);
237 SD.setPrivateExtern(true);
238 break;
239
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000240 case MCSA_WeakReference:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000241 // FIXME: This requires -dynamic.
242 if (Symbol->isUndefined())
243 SD.setFlags(SD.getFlags() | SF_WeakReference);
244 break;
245
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000246 case MCSA_WeakDefinition:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000247 // FIXME: 'as' enforces that this is defined and global. The manual claims
248 // it has to be in a coalesced section, but this isn't enforced.
249 SD.setFlags(SD.getFlags() | SF_WeakDefinition);
250 break;
Kevin Enderbyf59cac52010-07-08 17:22:42 +0000251
252 case MCSA_WeakDefAutoPrivate:
253 SD.setFlags(SD.getFlags() | SF_WeakDefinition | SF_WeakReference);
254 break;
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000255 }
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000256}
257
258void MCMachOStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000259 // Encode the 'desc' value into the lowest implementation defined bits.
Daniel Dunbar2ac0c452010-05-26 20:37:00 +0000260 assert(DescValue == (DescValue & SF_DescFlagsMask) &&
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000261 "Invalid .desc value!");
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000262 getAssembler().getOrCreateSymbolData(*Symbol).setFlags(
263 DescValue & SF_DescFlagsMask);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000264}
265
Chris Lattner9eb158d2010-01-23 07:47:02 +0000266void MCMachOStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000267 unsigned ByteAlignment) {
Daniel Dunbar8f4d1462009-08-28 07:08:35 +0000268 // FIXME: Darwin 'as' does appear to allow redef of a .comm by itself.
269 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
270
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000271 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
Daniel Dunbar8f4d1462009-08-28 07:08:35 +0000272 SD.setExternal(true);
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000273 SD.setCommon(Size, ByteAlignment);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000274}
275
Daniel Dunbar8751b942009-08-28 05:48:22 +0000276void MCMachOStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000277 unsigned Size, unsigned ByteAlignment) {
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000278 MCSectionData &SectData = getAssembler().getOrCreateSectionData(*Section);
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000279
280 // The symbol may not be present, which only creates the section.
281 if (!Symbol)
282 return;
283
284 // FIXME: Assert that this section has the zerofill type.
285
286 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
287
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000288 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000289
Daniel Dunbare73d49e2010-05-12 22:51:27 +0000290 // Emit an align fragment if necessary.
291 if (ByteAlignment != 1)
Daniel Dunbar1c154132010-05-12 22:56:23 +0000292 new MCAlignFragment(ByteAlignment, 0, 0, ByteAlignment, &SectData);
Daniel Dunbare73d49e2010-05-12 22:51:27 +0000293
Daniel Dunbar4e544872010-05-12 22:51:38 +0000294 MCFragment *F = new MCFillFragment(0, 0, Size, &SectData);
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000295 SD.setFragment(F);
296
297 Symbol->setSection(*Section);
298
299 // Update the maximum alignment on the zero fill section if necessary.
300 if (ByteAlignment > SectData.getAlignment())
301 SectData.setAlignment(ByteAlignment);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000302}
303
Eric Christopher070c1ab2010-05-21 23:03:53 +0000304// This should always be called with the thread local bss section. Like the
305// .zerofill directive this doesn't actually switch sections on us.
Eric Christopher011c3f12010-05-18 21:26:41 +0000306void MCMachOStreamer::EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
307 uint64_t Size, unsigned ByteAlignment) {
308 EmitZerofill(Section, Symbol, Size, ByteAlignment);
309 return;
Eric Christopher482eba02010-05-14 01:50:28 +0000310}
311
Chris Lattneraaec2052010-01-19 19:46:13 +0000312void MCMachOStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000313 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
314 // MCObjectStreamer.
Daniel Dunbarf70f4772010-03-22 20:35:46 +0000315 getOrCreateDataFragment()->getContents().append(Data.begin(), Data.end());
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000316}
317
Chris Lattneraaec2052010-01-19 19:46:13 +0000318void MCMachOStreamer::EmitValue(const MCExpr *Value, unsigned Size,
319 unsigned AddrSpace) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000320 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
321 // MCObjectStreamer.
Daniel Dunbarf70f4772010-03-22 20:35:46 +0000322 MCDataFragment *DF = getOrCreateDataFragment();
Daniel Dunbar45f48742010-02-13 09:28:22 +0000323
324 // Avoid fixups when possible.
325 int64_t AbsValue;
Daniel Dunbar40ebe2472010-02-22 22:08:57 +0000326 if (AddValueSymbols(Value)->EvaluateAsAbsolute(AbsValue)) {
Daniel Dunbar45f48742010-02-13 09:28:22 +0000327 // FIXME: Endianness assumption.
328 for (unsigned i = 0; i != Size; ++i)
329 DF->getContents().push_back(uint8_t(AbsValue >> (i * 8)));
330 } else {
Daniel Dunbarc90e30a2010-05-26 15:18:56 +0000331 DF->addFixup(MCFixup::Create(DF->getContents().size(),
332 AddValueSymbols(Value),
333 MCFixup::getKindForSize(Size)));
Daniel Dunbar45f48742010-02-13 09:28:22 +0000334 DF->getContents().resize(DF->getContents().size() + Size, 0);
335 }
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000336}
337
338void MCMachOStreamer::EmitValueToAlignment(unsigned ByteAlignment,
339 int64_t Value, unsigned ValueSize,
340 unsigned MaxBytesToEmit) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000341 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
342 // MCObjectStreamer.
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000343 if (MaxBytesToEmit == 0)
344 MaxBytesToEmit = ByteAlignment;
Daniel Dunbara86de102010-06-16 20:04:32 +0000345 new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit,
346 getCurrentSectionData());
Kevin Enderby6e720482010-02-23 18:26:34 +0000347
348 // Update the maximum alignment on the current section if necessary.
Daniel Dunbar83b46712010-06-16 20:04:25 +0000349 if (ByteAlignment > getCurrentSectionData()->getAlignment())
350 getCurrentSectionData()->setAlignment(ByteAlignment);
Kevin Enderby6e720482010-02-23 18:26:34 +0000351}
352
353void MCMachOStreamer::EmitCodeAlignment(unsigned ByteAlignment,
354 unsigned MaxBytesToEmit) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000355 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
356 // MCObjectStreamer.
Kevin Enderby6e720482010-02-23 18:26:34 +0000357 if (MaxBytesToEmit == 0)
358 MaxBytesToEmit = ByteAlignment;
Daniel Dunbar1c154132010-05-12 22:56:23 +0000359 MCAlignFragment *F = new MCAlignFragment(ByteAlignment, 0, 1, MaxBytesToEmit,
Daniel Dunbar83b46712010-06-16 20:04:25 +0000360 getCurrentSectionData());
Daniel Dunbar1c154132010-05-12 22:56:23 +0000361 F->setEmitNops(true);
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000362
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000363 // Update the maximum alignment on the current section if necessary.
Daniel Dunbar83b46712010-06-16 20:04:25 +0000364 if (ByteAlignment > getCurrentSectionData()->getAlignment())
365 getCurrentSectionData()->setAlignment(ByteAlignment);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000366}
367
Daniel Dunbar821e3332009-08-31 08:09:28 +0000368void MCMachOStreamer::EmitValueToOffset(const MCExpr *Offset,
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000369 unsigned char Value) {
Daniel Dunbara86de102010-06-16 20:04:32 +0000370 new MCOrgFragment(*Offset, Value, getCurrentSectionData());
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000371}
372
Daniel Dunbar2ac0c452010-05-26 20:37:00 +0000373void MCMachOStreamer::EmitInstToFragment(const MCInst &Inst) {
Daniel Dunbar83b46712010-06-16 20:04:25 +0000374 MCInstFragment *IF = new MCInstFragment(Inst, getCurrentSectionData());
Daniel Dunbar2ac0c452010-05-26 20:37:00 +0000375
376 // Add the fixups and data.
377 //
378 // FIXME: Revisit this design decision when relaxation is done, we may be
379 // able to get away with not storing any extra data in the MCInst.
380 SmallVector<MCFixup, 4> Fixups;
381 SmallString<256> Code;
382 raw_svector_ostream VecOS(Code);
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000383 getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups);
Daniel Dunbar2ac0c452010-05-26 20:37:00 +0000384 VecOS.flush();
385
386 IF->getCode() = Code;
387 IF->getFixups() = Fixups;
388}
389
390void MCMachOStreamer::EmitInstToData(const MCInst &Inst) {
391 MCDataFragment *DF = getOrCreateDataFragment();
392
393 SmallVector<MCFixup, 4> Fixups;
394 SmallString<256> Code;
395 raw_svector_ostream VecOS(Code);
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000396 getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups);
Daniel Dunbar2ac0c452010-05-26 20:37:00 +0000397 VecOS.flush();
398
399 // Add the fixups and data.
400 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
401 Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size());
402 DF->addFixup(Fixups[i]);
403 }
404 DF->getContents().append(Code.begin(), Code.end());
405}
406
Daniel Dunbara86de102010-06-16 20:04:32 +0000407void MCMachOStreamer::Finish() {
Kevin Enderbyc0957932010-09-30 16:52:03 +0000408 // Dump out the dwarf file & directory tables and line tables.
Kevin Enderby8c9aa922010-10-02 00:13:41 +0000409 if (getContext().hasDwarfFiles()) {
Kevin Enderbyc0957932010-09-30 16:52:03 +0000410 const MCSection *DwarfLineSection = getContext().getMachOSection("__DWARF",
411 "__debug_line",
412 MCSectionMachO::S_ATTR_DEBUG,
413 0, SectionKind::getDataRelLocal());
414 MCDwarfFileTable::Emit(this, DwarfLineSection);
415 }
Kevin Enderbyb07ce602010-08-09 22:52:14 +0000416
Daniel Dunbara86de102010-06-16 20:04:32 +0000417 // We have to set the fragment atom associations so we can relax properly for
418 // Mach-O.
419
420 // First, scan the symbol table to build a lookup table from fragments to
421 // defining symbols.
422 DenseMap<const MCFragment*, MCSymbolData*> DefiningSymbolMap;
423 for (MCAssembler::symbol_iterator it = getAssembler().symbol_begin(),
424 ie = getAssembler().symbol_end(); it != ie; ++it) {
425 if (getAssembler().isSymbolLinkerVisible(it->getSymbol()) &&
426 it->getFragment()) {
427 // An atom defining symbol should never be internal to a fragment.
428 assert(it->getOffset() == 0 && "Invalid offset in atom defining symbol!");
429 DefiningSymbolMap[it->getFragment()] = it;
430 }
431 }
432
433 // Set the fragment atom associations by tracking the last seen atom defining
434 // symbol.
435 for (MCAssembler::iterator it = getAssembler().begin(),
436 ie = getAssembler().end(); it != ie; ++it) {
437 MCSymbolData *CurrentAtom = 0;
438 for (MCSectionData::iterator it2 = it->begin(),
439 ie2 = it->end(); it2 != ie2; ++it2) {
440 if (MCSymbolData *SD = DefiningSymbolMap.lookup(it2))
441 CurrentAtom = SD;
442 it2->setAtom(CurrentAtom);
443 }
444 }
445
446 this->MCObjectStreamer::Finish();
447}
448
Daniel Dunbar1f3e4452010-03-11 01:34:27 +0000449MCStreamer *llvm::createMachOStreamer(MCContext &Context, TargetAsmBackend &TAB,
Daniel Dunbarac2884a2010-03-25 22:49:09 +0000450 raw_ostream &OS, MCCodeEmitter *CE,
451 bool RelaxAll) {
452 MCMachOStreamer *S = new MCMachOStreamer(Context, TAB, OS, CE);
453 if (RelaxAll)
454 S->getAssembler().setRelaxAll(true);
455 return S;
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000456}