blob: b8640d3b08e1420342cece9dec2866e9abf0bffa [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);
Jim Grosbachce792992010-11-05 22:08:08 +000048 virtual void EmitThumbFunc(MCSymbol *Func);
Daniel Dunbar821e3332009-08-31 08:09:28 +000049 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value);
Chris Lattnera5ad93a2010-01-23 06:39:22 +000050 virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000051 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
Chris Lattner9eb158d2010-01-23 07:47:02 +000052 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +000053 unsigned ByteAlignment);
Chris Lattnerb54b9dd2010-05-08 19:54:22 +000054 virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol) {
55 assert(0 && "macho doesn't support this directive");
56 }
57 virtual void EmitCOFFSymbolStorageClass(int StorageClass) {
58 assert(0 && "macho doesn't support this directive");
59 }
60 virtual void EmitCOFFSymbolType(int Type) {
61 assert(0 && "macho doesn't support this directive");
62 }
63 virtual void EndCOFFSymbolDef() {
64 assert(0 && "macho doesn't support this directive");
65 }
Chris Lattner99328ad2010-01-25 07:52:13 +000066 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
67 assert(0 && "macho doesn't support this directive");
68 }
Chris Lattner9eb158d2010-01-23 07:47:02 +000069 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size) {
70 assert(0 && "macho doesn't support this directive");
71 }
Daniel Dunbar8751b942009-08-28 05:48:22 +000072 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +000073 unsigned Size = 0, unsigned ByteAlignment = 0);
Eric Christopher011c3f12010-05-18 21:26:41 +000074 virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
75 uint64_t Size, unsigned ByteAlignment = 0);
Chris Lattneraaec2052010-01-19 19:46:13 +000076 virtual void EmitBytes(StringRef Data, unsigned AddrSpace);
Chris Lattneraaec2052010-01-19 19:46:13 +000077 virtual void EmitValue(const MCExpr *Value, unsigned Size,unsigned AddrSpace);
Chris Lattner718fb592010-01-25 21:28:50 +000078 virtual void EmitGPRel32Value(const MCExpr *Value) {
79 assert(0 && "macho doesn't support this directive");
80 }
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000081 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
82 unsigned ValueSize = 1,
83 unsigned MaxBytesToEmit = 0);
Kevin Enderby6e720482010-02-23 18:26:34 +000084 virtual void EmitCodeAlignment(unsigned ByteAlignment,
85 unsigned MaxBytesToEmit = 0);
Daniel Dunbar821e3332009-08-31 08:09:28 +000086 virtual void EmitValueToOffset(const MCExpr *Offset,
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000087 unsigned char Value = 0);
Daniel Dunbar2ac0c452010-05-26 20:37:00 +000088
Chris Lattnera6594fc2010-01-25 18:58:59 +000089 virtual void EmitFileDirective(StringRef Filename) {
Daniel Dunbar96749672010-07-19 20:44:20 +000090 // FIXME: Just ignore the .file; it isn't important enough to fail the
91 // entire assembly.
92
93 //report_fatal_error("unsupported directive: '.file'");
Chris Lattnera6594fc2010-01-25 18:58:59 +000094 }
95 virtual void EmitDwarfFileDirective(unsigned FileNo, StringRef Filename) {
Daniel Dunbar96749672010-07-19 20:44:20 +000096 // FIXME: Just ignore the .file; it isn't important enough to fail the
97 // entire assembly.
98
99 //report_fatal_error("unsupported directive: '.file'");
Chris Lattnera6594fc2010-01-25 18:58:59 +0000100 }
Daniel Dunbar2ac0c452010-05-26 20:37:00 +0000101
Daniel Dunbara86de102010-06-16 20:04:32 +0000102 virtual void Finish();
103
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000104 /// @}
105};
106
107} // end anonymous namespace.
108
Rafael Espindolad80781b2010-09-15 21:48:40 +0000109void MCMachOStreamer::InitSections() {
110 SwitchSection(getContext().getMachOSection("__TEXT", "__text",
111 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
112 0, SectionKind::getText()));
113
114}
115
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000116void MCMachOStreamer::EmitLabel(MCSymbol *Symbol) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000117 // TODO: This is almost exactly the same as WinCOFFStreamer. Consider merging
118 // into MCObjectStreamer.
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000119 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
Daniel Dunbarc3047182010-05-05 19:01:00 +0000120 assert(!Symbol->isVariable() && "Cannot emit a variable symbol!");
121 assert(CurSection && "Cannot emit before setting section!");
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000122
Daniel Dunbara86de102010-06-16 20:04:32 +0000123 Symbol->setSection(*CurSection);
124
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000125 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
Daniel Dunbar071f73d2010-05-10 22:45:09 +0000126
Daniel Dunbara86de102010-06-16 20:04:32 +0000127 // We have to create a new fragment if this is an atom defining symbol,
128 // fragments cannot span atoms.
129 if (getAssembler().isSymbolLinkerVisible(SD.getSymbol()))
130 new MCDataFragment(getCurrentSectionData());
Daniel Dunbar071f73d2010-05-10 22:45:09 +0000131
Daniel Dunbarf70f4772010-03-22 20:35:46 +0000132 // FIXME: This is wasteful, we don't necessarily need to create a data
133 // fragment. Instead, we should mark the symbol as pointing into the data
134 // fragment if it exists, otherwise we should just queue the label and set its
135 // fragment pointer when we emit the next fragment.
Daniel Dunbara86de102010-06-16 20:04:32 +0000136 MCDataFragment *F = getOrCreateDataFragment();
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000137 assert(!SD.getFragment() && "Unexpected fragment on symbol data!");
138 SD.setFragment(F);
139 SD.setOffset(F->getContents().size());
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000140
Daniel Dunbarb18d2dd2010-05-17 20:12:31 +0000141 // This causes the reference type flag to be cleared. Darwin 'as' was "trying"
142 // to clear the weak reference and weak definition bits too, but the
143 // implementation was buggy. For now we just try to match 'as', for
144 // diffability.
145 //
146 // FIXME: Cleanup this code, these bits should be emitted based on semantic
147 // properties, not on the order of definition, etc.
148 SD.setFlags(SD.getFlags() & ~SF_ReferenceTypeMask);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000149}
150
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000151void MCMachOStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
Daniel Dunbar6009db42009-08-26 21:22:22 +0000152 switch (Flag) {
Jim Grosbachce792992010-11-05 22:08:08 +0000153 case MCAF_SyntaxUnified: return; // no-op here.
154 case MCAF_Code16: return; // no-op here.
Jim Grosbachba219572010-11-05 22:40:09 +0000155 case MCAF_Code32: return; // no-op here.
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000156 case MCAF_SubsectionsViaSymbols:
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000157 getAssembler().setSubsectionsViaSymbols(true);
Daniel Dunbar8c3eaf42009-08-28 07:08:47 +0000158 return;
Chandler Carruth5c0d4722010-09-30 10:59:51 +0000159 default:
160 llvm_unreachable("invalid assembler flag!");
Daniel Dunbar6009db42009-08-26 21:22:22 +0000161 }
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000162}
163
Jim Grosbachce792992010-11-05 22:08:08 +0000164void MCMachOStreamer::EmitThumbFunc(MCSymbol *Func) {
165 // FIXME: Flag the function ISA as thumb with DW_AT_APPLE_isa.
166}
167
Daniel Dunbar821e3332009-08-31 08:09:28 +0000168void MCMachOStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000169 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
170 // MCObjectStreamer.
Daniel Dunbara4d86672009-10-16 01:58:23 +0000171 // FIXME: Lift context changes into super class.
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000172 getAssembler().getOrCreateSymbolData(*Symbol);
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000173 Symbol->setVariableValue(AddValueSymbols(Value));
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000174}
175
176void MCMachOStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000177 MCSymbolAttr Attribute) {
Daniel Dunbar0c7761b2009-08-24 11:56:58 +0000178 // Indirect symbols are handled differently, to match how 'as' handles
179 // them. This makes writing matching .o files easier.
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000180 if (Attribute == MCSA_IndirectSymbol) {
Daniel Dunbarad7c3d52009-08-26 00:18:21 +0000181 // Note that we intentionally cannot use the symbol data here; this is
182 // important for matching the string table that 'as' generates.
Daniel Dunbar0c7761b2009-08-24 11:56:58 +0000183 IndirectSymbolData ISD;
184 ISD.Symbol = Symbol;
Daniel Dunbar83b46712010-06-16 20:04:25 +0000185 ISD.SectionData = getCurrentSectionData();
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000186 getAssembler().getIndirectSymbols().push_back(ISD);
Daniel Dunbar0c7761b2009-08-24 11:56:58 +0000187 return;
188 }
189
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000190 // Adding a symbol attribute always introduces the symbol, note that an
Daniel Dunbar46836a72010-03-10 20:58:29 +0000191 // important side effect of calling getOrCreateSymbolData here is to register
192 // the symbol with the assembler.
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000193 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000194
195 // The implementation of symbol attributes is designed to match 'as', but it
196 // leaves much to desired. It doesn't really make sense to arbitrarily add and
197 // remove flags, but 'as' allows this (in particular, see .desc).
198 //
199 // In the future it might be worth trying to make these operations more well
200 // defined.
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000201 switch (Attribute) {
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000202 case MCSA_Invalid:
Chris Lattnered0ab152010-01-25 18:30:45 +0000203 case MCSA_ELF_TypeFunction:
204 case MCSA_ELF_TypeIndFunction:
205 case MCSA_ELF_TypeObject:
206 case MCSA_ELF_TypeTLS:
207 case MCSA_ELF_TypeCommon:
208 case MCSA_ELF_TypeNoType:
Rafael Espindolae13a0ff2010-11-13 04:51:02 +0000209 case MCSA_ELF_TypeGnuUniqueObject:
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000210 case MCSA_IndirectSymbol:
211 case MCSA_Hidden:
212 case MCSA_Internal:
213 case MCSA_Protected:
214 case MCSA_Weak:
215 case MCSA_Local:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000216 assert(0 && "Invalid symbol attribute for Mach-O!");
217 break;
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000218
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000219 case MCSA_Global:
Daniel Dunbar8f4d1462009-08-28 07:08:35 +0000220 SD.setExternal(true);
Daniel Dunbarb18d2dd2010-05-17 20:12:31 +0000221 // This effectively clears the undefined lazy bit, in Darwin 'as', although
222 // it isn't very consistent because it implements this as part of symbol
223 // lookup.
224 //
225 // FIXME: Cleanup this code, these bits should be emitted based on semantic
226 // properties, not on the order of definition, etc.
227 SD.setFlags(SD.getFlags() & ~SF_ReferenceTypeUndefinedLazy);
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000228 break;
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000229
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000230 case MCSA_LazyReference:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000231 // FIXME: This requires -dynamic.
232 SD.setFlags(SD.getFlags() | SF_NoDeadStrip);
233 if (Symbol->isUndefined())
234 SD.setFlags(SD.getFlags() | SF_ReferenceTypeUndefinedLazy);
235 break;
236
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000237 // Since .reference sets the no dead strip bit, it is equivalent to
238 // .no_dead_strip in practice.
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000239 case MCSA_Reference:
240 case MCSA_NoDeadStrip:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000241 SD.setFlags(SD.getFlags() | SF_NoDeadStrip);
242 break;
243
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000244 case MCSA_PrivateExtern:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000245 SD.setExternal(true);
246 SD.setPrivateExtern(true);
247 break;
248
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000249 case MCSA_WeakReference:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000250 // FIXME: This requires -dynamic.
251 if (Symbol->isUndefined())
252 SD.setFlags(SD.getFlags() | SF_WeakReference);
253 break;
254
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000255 case MCSA_WeakDefinition:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000256 // FIXME: 'as' enforces that this is defined and global. The manual claims
257 // it has to be in a coalesced section, but this isn't enforced.
258 SD.setFlags(SD.getFlags() | SF_WeakDefinition);
259 break;
Kevin Enderbyf59cac52010-07-08 17:22:42 +0000260
261 case MCSA_WeakDefAutoPrivate:
262 SD.setFlags(SD.getFlags() | SF_WeakDefinition | SF_WeakReference);
263 break;
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000264 }
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000265}
266
267void MCMachOStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000268 // Encode the 'desc' value into the lowest implementation defined bits.
Daniel Dunbar2ac0c452010-05-26 20:37:00 +0000269 assert(DescValue == (DescValue & SF_DescFlagsMask) &&
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000270 "Invalid .desc value!");
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000271 getAssembler().getOrCreateSymbolData(*Symbol).setFlags(
272 DescValue & SF_DescFlagsMask);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000273}
274
Chris Lattner9eb158d2010-01-23 07:47:02 +0000275void MCMachOStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000276 unsigned ByteAlignment) {
Daniel Dunbar8f4d1462009-08-28 07:08:35 +0000277 // FIXME: Darwin 'as' does appear to allow redef of a .comm by itself.
278 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
279
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000280 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
Daniel Dunbar8f4d1462009-08-28 07:08:35 +0000281 SD.setExternal(true);
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000282 SD.setCommon(Size, ByteAlignment);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000283}
284
Daniel Dunbar8751b942009-08-28 05:48:22 +0000285void MCMachOStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000286 unsigned Size, unsigned ByteAlignment) {
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000287 MCSectionData &SectData = getAssembler().getOrCreateSectionData(*Section);
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000288
289 // The symbol may not be present, which only creates the section.
290 if (!Symbol)
291 return;
292
293 // FIXME: Assert that this section has the zerofill type.
294
295 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
296
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000297 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000298
Daniel Dunbare73d49e2010-05-12 22:51:27 +0000299 // Emit an align fragment if necessary.
300 if (ByteAlignment != 1)
Daniel Dunbar1c154132010-05-12 22:56:23 +0000301 new MCAlignFragment(ByteAlignment, 0, 0, ByteAlignment, &SectData);
Daniel Dunbare73d49e2010-05-12 22:51:27 +0000302
Daniel Dunbar4e544872010-05-12 22:51:38 +0000303 MCFragment *F = new MCFillFragment(0, 0, Size, &SectData);
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000304 SD.setFragment(F);
305
306 Symbol->setSection(*Section);
307
308 // Update the maximum alignment on the zero fill section if necessary.
309 if (ByteAlignment > SectData.getAlignment())
310 SectData.setAlignment(ByteAlignment);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000311}
312
Eric Christopher070c1ab2010-05-21 23:03:53 +0000313// This should always be called with the thread local bss section. Like the
314// .zerofill directive this doesn't actually switch sections on us.
Eric Christopher011c3f12010-05-18 21:26:41 +0000315void MCMachOStreamer::EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
316 uint64_t Size, unsigned ByteAlignment) {
317 EmitZerofill(Section, Symbol, Size, ByteAlignment);
318 return;
Eric Christopher482eba02010-05-14 01:50:28 +0000319}
320
Chris Lattneraaec2052010-01-19 19:46:13 +0000321void MCMachOStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000322 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
323 // MCObjectStreamer.
Daniel Dunbarf70f4772010-03-22 20:35:46 +0000324 getOrCreateDataFragment()->getContents().append(Data.begin(), Data.end());
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000325}
326
Chris Lattneraaec2052010-01-19 19:46:13 +0000327void MCMachOStreamer::EmitValue(const MCExpr *Value, unsigned Size,
328 unsigned AddrSpace) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000329 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
330 // MCObjectStreamer.
Daniel Dunbarf70f4772010-03-22 20:35:46 +0000331 MCDataFragment *DF = getOrCreateDataFragment();
Daniel Dunbar45f48742010-02-13 09:28:22 +0000332
333 // Avoid fixups when possible.
334 int64_t AbsValue;
Daniel Dunbar40ebe2472010-02-22 22:08:57 +0000335 if (AddValueSymbols(Value)->EvaluateAsAbsolute(AbsValue)) {
Daniel Dunbar45f48742010-02-13 09:28:22 +0000336 // FIXME: Endianness assumption.
337 for (unsigned i = 0; i != Size; ++i)
338 DF->getContents().push_back(uint8_t(AbsValue >> (i * 8)));
339 } else {
Daniel Dunbarc90e30a2010-05-26 15:18:56 +0000340 DF->addFixup(MCFixup::Create(DF->getContents().size(),
341 AddValueSymbols(Value),
342 MCFixup::getKindForSize(Size)));
Daniel Dunbar45f48742010-02-13 09:28:22 +0000343 DF->getContents().resize(DF->getContents().size() + Size, 0);
344 }
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000345}
346
347void MCMachOStreamer::EmitValueToAlignment(unsigned ByteAlignment,
348 int64_t Value, unsigned ValueSize,
349 unsigned MaxBytesToEmit) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000350 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
351 // MCObjectStreamer.
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000352 if (MaxBytesToEmit == 0)
353 MaxBytesToEmit = ByteAlignment;
Daniel Dunbara86de102010-06-16 20:04:32 +0000354 new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit,
355 getCurrentSectionData());
Kevin Enderby6e720482010-02-23 18:26:34 +0000356
357 // Update the maximum alignment on the current section if necessary.
Daniel Dunbar83b46712010-06-16 20:04:25 +0000358 if (ByteAlignment > getCurrentSectionData()->getAlignment())
359 getCurrentSectionData()->setAlignment(ByteAlignment);
Kevin Enderby6e720482010-02-23 18:26:34 +0000360}
361
362void MCMachOStreamer::EmitCodeAlignment(unsigned ByteAlignment,
363 unsigned MaxBytesToEmit) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000364 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
365 // MCObjectStreamer.
Kevin Enderby6e720482010-02-23 18:26:34 +0000366 if (MaxBytesToEmit == 0)
367 MaxBytesToEmit = ByteAlignment;
Daniel Dunbar1c154132010-05-12 22:56:23 +0000368 MCAlignFragment *F = new MCAlignFragment(ByteAlignment, 0, 1, MaxBytesToEmit,
Daniel Dunbar83b46712010-06-16 20:04:25 +0000369 getCurrentSectionData());
Daniel Dunbar1c154132010-05-12 22:56:23 +0000370 F->setEmitNops(true);
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000371
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000372 // Update the maximum alignment on the current section if necessary.
Daniel Dunbar83b46712010-06-16 20:04:25 +0000373 if (ByteAlignment > getCurrentSectionData()->getAlignment())
374 getCurrentSectionData()->setAlignment(ByteAlignment);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000375}
376
Daniel Dunbar821e3332009-08-31 08:09:28 +0000377void MCMachOStreamer::EmitValueToOffset(const MCExpr *Offset,
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000378 unsigned char Value) {
Daniel Dunbara86de102010-06-16 20:04:32 +0000379 new MCOrgFragment(*Offset, Value, getCurrentSectionData());
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000380}
381
Daniel Dunbar2ac0c452010-05-26 20:37:00 +0000382void MCMachOStreamer::EmitInstToFragment(const MCInst &Inst) {
Daniel Dunbar83b46712010-06-16 20:04:25 +0000383 MCInstFragment *IF = new MCInstFragment(Inst, getCurrentSectionData());
Daniel Dunbar2ac0c452010-05-26 20:37:00 +0000384
385 // Add the fixups and data.
386 //
387 // FIXME: Revisit this design decision when relaxation is done, we may be
388 // able to get away with not storing any extra data in the MCInst.
389 SmallVector<MCFixup, 4> Fixups;
390 SmallString<256> Code;
391 raw_svector_ostream VecOS(Code);
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000392 getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups);
Daniel Dunbar2ac0c452010-05-26 20:37:00 +0000393 VecOS.flush();
394
395 IF->getCode() = Code;
396 IF->getFixups() = Fixups;
397}
398
399void MCMachOStreamer::EmitInstToData(const MCInst &Inst) {
400 MCDataFragment *DF = getOrCreateDataFragment();
401
402 SmallVector<MCFixup, 4> Fixups;
403 SmallString<256> Code;
404 raw_svector_ostream VecOS(Code);
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000405 getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups);
Daniel Dunbar2ac0c452010-05-26 20:37:00 +0000406 VecOS.flush();
407
408 // Add the fixups and data.
409 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
410 Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size());
411 DF->addFixup(Fixups[i]);
412 }
413 DF->getContents().append(Code.begin(), Code.end());
414}
415
Daniel Dunbara86de102010-06-16 20:04:32 +0000416void MCMachOStreamer::Finish() {
Kevin Enderbyc0957932010-09-30 16:52:03 +0000417 // Dump out the dwarf file & directory tables and line tables.
Kevin Enderby8c9aa922010-10-02 00:13:41 +0000418 if (getContext().hasDwarfFiles()) {
Kevin Enderbyc0957932010-09-30 16:52:03 +0000419 const MCSection *DwarfLineSection = getContext().getMachOSection("__DWARF",
420 "__debug_line",
421 MCSectionMachO::S_ATTR_DEBUG,
422 0, SectionKind::getDataRelLocal());
423 MCDwarfFileTable::Emit(this, DwarfLineSection);
424 }
Kevin Enderbyb07ce602010-08-09 22:52:14 +0000425
Daniel Dunbara86de102010-06-16 20:04:32 +0000426 // We have to set the fragment atom associations so we can relax properly for
427 // Mach-O.
428
429 // First, scan the symbol table to build a lookup table from fragments to
430 // defining symbols.
431 DenseMap<const MCFragment*, MCSymbolData*> DefiningSymbolMap;
432 for (MCAssembler::symbol_iterator it = getAssembler().symbol_begin(),
433 ie = getAssembler().symbol_end(); it != ie; ++it) {
434 if (getAssembler().isSymbolLinkerVisible(it->getSymbol()) &&
435 it->getFragment()) {
436 // An atom defining symbol should never be internal to a fragment.
437 assert(it->getOffset() == 0 && "Invalid offset in atom defining symbol!");
438 DefiningSymbolMap[it->getFragment()] = it;
439 }
440 }
441
442 // Set the fragment atom associations by tracking the last seen atom defining
443 // symbol.
444 for (MCAssembler::iterator it = getAssembler().begin(),
445 ie = getAssembler().end(); it != ie; ++it) {
446 MCSymbolData *CurrentAtom = 0;
447 for (MCSectionData::iterator it2 = it->begin(),
448 ie2 = it->end(); it2 != ie2; ++it2) {
449 if (MCSymbolData *SD = DefiningSymbolMap.lookup(it2))
450 CurrentAtom = SD;
451 it2->setAtom(CurrentAtom);
452 }
453 }
454
455 this->MCObjectStreamer::Finish();
456}
457
Daniel Dunbar1f3e4452010-03-11 01:34:27 +0000458MCStreamer *llvm::createMachOStreamer(MCContext &Context, TargetAsmBackend &TAB,
Daniel Dunbarac2884a2010-03-25 22:49:09 +0000459 raw_ostream &OS, MCCodeEmitter *CE,
460 bool RelaxAll) {
461 MCMachOStreamer *S = new MCMachOStreamer(Context, TAB, OS, CE);
462 if (RelaxAll)
463 S->getAssembler().setRelaxAll(true);
464 return S;
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000465}