blob: 88d9982bd2afe02f324ced1bf4142e283eefab24 [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"
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000023#include "llvm/Support/ErrorHandling.h"
Daniel Dunbar4fac7492009-08-27 08:17:51 +000024#include "llvm/Support/raw_ostream.h"
Daniel Dunbard8036fb2010-03-23 05:09:03 +000025#include "llvm/Target/TargetAsmBackend.h"
26
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000027using namespace llvm;
28
29namespace {
30
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +000031class MCMachOStreamer : public MCObjectStreamer {
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +000032private:
Daniel Dunbar2ac0c452010-05-26 20:37:00 +000033 void EmitInstToFragment(const MCInst &Inst);
34 void EmitInstToData(const MCInst &Inst);
Kevin Enderbyb07ce602010-08-09 22:52:14 +000035 // FIXME: These will likely moved to a better place.
36 const MCExpr * MakeStartMinusEndExpr(MCSymbol *Start, MCSymbol *End,
37 int IntVal);
38 void EmitDwarfFileTable(void);
Daniel Dunbar2ac0c452010-05-26 20:37:00 +000039
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000040public:
Daniel Dunbar1f3e4452010-03-11 01:34:27 +000041 MCMachOStreamer(MCContext &Context, TargetAsmBackend &TAB,
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +000042 raw_ostream &OS, MCCodeEmitter *Emitter)
Daniel Dunbar83b46712010-06-16 20:04:25 +000043 : MCObjectStreamer(Context, TAB, OS, Emitter) {}
Daniel Dunbarac2884a2010-03-25 22:49:09 +000044
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000045 /// @name MCStreamer Interface
46 /// @{
47
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000048 virtual void EmitLabel(MCSymbol *Symbol);
Chris Lattnera5ad93a2010-01-23 06:39:22 +000049 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag);
Daniel Dunbar821e3332009-08-31 08:09:28 +000050 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value);
Chris Lattnera5ad93a2010-01-23 06:39:22 +000051 virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000052 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
Chris Lattner9eb158d2010-01-23 07:47:02 +000053 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +000054 unsigned ByteAlignment);
Chris Lattnerb54b9dd2010-05-08 19:54:22 +000055 virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol) {
56 assert(0 && "macho doesn't support this directive");
57 }
58 virtual void EmitCOFFSymbolStorageClass(int StorageClass) {
59 assert(0 && "macho doesn't support this directive");
60 }
61 virtual void EmitCOFFSymbolType(int Type) {
62 assert(0 && "macho doesn't support this directive");
63 }
64 virtual void EndCOFFSymbolDef() {
65 assert(0 && "macho doesn't support this directive");
66 }
Chris Lattner99328ad2010-01-25 07:52:13 +000067 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
68 assert(0 && "macho doesn't support this directive");
69 }
Chris Lattner9eb158d2010-01-23 07:47:02 +000070 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size) {
71 assert(0 && "macho doesn't support this directive");
72 }
Daniel Dunbar8751b942009-08-28 05:48:22 +000073 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +000074 unsigned Size = 0, unsigned ByteAlignment = 0);
Eric Christopher011c3f12010-05-18 21:26:41 +000075 virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
76 uint64_t Size, unsigned ByteAlignment = 0);
Chris Lattneraaec2052010-01-19 19:46:13 +000077 virtual void EmitBytes(StringRef Data, unsigned AddrSpace);
Chris Lattneraaec2052010-01-19 19:46:13 +000078 virtual void EmitValue(const MCExpr *Value, unsigned Size,unsigned AddrSpace);
Chris Lattner718fb592010-01-25 21:28:50 +000079 virtual void EmitGPRel32Value(const MCExpr *Value) {
80 assert(0 && "macho doesn't support this directive");
81 }
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000082 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
83 unsigned ValueSize = 1,
84 unsigned MaxBytesToEmit = 0);
Kevin Enderby6e720482010-02-23 18:26:34 +000085 virtual void EmitCodeAlignment(unsigned ByteAlignment,
86 unsigned MaxBytesToEmit = 0);
Daniel Dunbar821e3332009-08-31 08:09:28 +000087 virtual void EmitValueToOffset(const MCExpr *Offset,
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000088 unsigned char Value = 0);
Daniel Dunbar2ac0c452010-05-26 20:37:00 +000089
Chris Lattnera6594fc2010-01-25 18:58:59 +000090 virtual void EmitFileDirective(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 }
96 virtual void EmitDwarfFileDirective(unsigned FileNo, StringRef Filename) {
Daniel Dunbar96749672010-07-19 20:44:20 +000097 // FIXME: Just ignore the .file; it isn't important enough to fail the
98 // entire assembly.
99
100 //report_fatal_error("unsupported directive: '.file'");
Chris Lattnera6594fc2010-01-25 18:58:59 +0000101 }
Daniel Dunbar2ac0c452010-05-26 20:37:00 +0000102
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000103 virtual void EmitInstruction(const MCInst &Inst);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000104
Daniel Dunbara86de102010-06-16 20:04:32 +0000105 virtual void Finish();
106
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000107 /// @}
108};
109
110} // end anonymous namespace.
111
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000112void MCMachOStreamer::EmitLabel(MCSymbol *Symbol) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000113 // TODO: This is almost exactly the same as WinCOFFStreamer. Consider merging
114 // into MCObjectStreamer.
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000115 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
Daniel Dunbarc3047182010-05-05 19:01:00 +0000116 assert(!Symbol->isVariable() && "Cannot emit a variable symbol!");
117 assert(CurSection && "Cannot emit before setting section!");
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000118
Daniel Dunbara86de102010-06-16 20:04:32 +0000119 Symbol->setSection(*CurSection);
120
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000121 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
Daniel Dunbar071f73d2010-05-10 22:45:09 +0000122
Daniel Dunbara86de102010-06-16 20:04:32 +0000123 // We have to create a new fragment if this is an atom defining symbol,
124 // fragments cannot span atoms.
125 if (getAssembler().isSymbolLinkerVisible(SD.getSymbol()))
126 new MCDataFragment(getCurrentSectionData());
Daniel Dunbar071f73d2010-05-10 22:45:09 +0000127
Daniel Dunbarf70f4772010-03-22 20:35:46 +0000128 // FIXME: This is wasteful, we don't necessarily need to create a data
129 // fragment. Instead, we should mark the symbol as pointing into the data
130 // fragment if it exists, otherwise we should just queue the label and set its
131 // fragment pointer when we emit the next fragment.
Daniel Dunbara86de102010-06-16 20:04:32 +0000132 MCDataFragment *F = getOrCreateDataFragment();
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000133 assert(!SD.getFragment() && "Unexpected fragment on symbol data!");
134 SD.setFragment(F);
135 SD.setOffset(F->getContents().size());
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000136
Daniel Dunbarb18d2dd2010-05-17 20:12:31 +0000137 // This causes the reference type flag to be cleared. Darwin 'as' was "trying"
138 // to clear the weak reference and weak definition bits too, but the
139 // implementation was buggy. For now we just try to match 'as', for
140 // diffability.
141 //
142 // FIXME: Cleanup this code, these bits should be emitted based on semantic
143 // properties, not on the order of definition, etc.
144 SD.setFlags(SD.getFlags() & ~SF_ReferenceTypeMask);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000145}
146
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000147void MCMachOStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
Daniel Dunbar6009db42009-08-26 21:22:22 +0000148 switch (Flag) {
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000149 case MCAF_SubsectionsViaSymbols:
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000150 getAssembler().setSubsectionsViaSymbols(true);
Daniel Dunbar8c3eaf42009-08-28 07:08:47 +0000151 return;
Daniel Dunbar6009db42009-08-26 21:22:22 +0000152 }
Daniel Dunbar8c3eaf42009-08-28 07:08:47 +0000153
154 assert(0 && "invalid assembler flag!");
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000155}
156
Daniel Dunbar821e3332009-08-31 08:09:28 +0000157void MCMachOStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000158 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
159 // MCObjectStreamer.
Daniel Dunbara4d86672009-10-16 01:58:23 +0000160 // FIXME: Lift context changes into super class.
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000161 getAssembler().getOrCreateSymbolData(*Symbol);
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000162 Symbol->setVariableValue(AddValueSymbols(Value));
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000163}
164
165void MCMachOStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000166 MCSymbolAttr Attribute) {
Daniel Dunbar0c7761b2009-08-24 11:56:58 +0000167 // Indirect symbols are handled differently, to match how 'as' handles
168 // them. This makes writing matching .o files easier.
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000169 if (Attribute == MCSA_IndirectSymbol) {
Daniel Dunbarad7c3d52009-08-26 00:18:21 +0000170 // Note that we intentionally cannot use the symbol data here; this is
171 // important for matching the string table that 'as' generates.
Daniel Dunbar0c7761b2009-08-24 11:56:58 +0000172 IndirectSymbolData ISD;
173 ISD.Symbol = Symbol;
Daniel Dunbar83b46712010-06-16 20:04:25 +0000174 ISD.SectionData = getCurrentSectionData();
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000175 getAssembler().getIndirectSymbols().push_back(ISD);
Daniel Dunbar0c7761b2009-08-24 11:56:58 +0000176 return;
177 }
178
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000179 // Adding a symbol attribute always introduces the symbol, note that an
Daniel Dunbar46836a72010-03-10 20:58:29 +0000180 // important side effect of calling getOrCreateSymbolData here is to register
181 // the symbol with the assembler.
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000182 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000183
184 // The implementation of symbol attributes is designed to match 'as', but it
185 // leaves much to desired. It doesn't really make sense to arbitrarily add and
186 // remove flags, but 'as' allows this (in particular, see .desc).
187 //
188 // In the future it might be worth trying to make these operations more well
189 // defined.
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000190 switch (Attribute) {
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000191 case MCSA_Invalid:
Chris Lattnered0ab152010-01-25 18:30:45 +0000192 case MCSA_ELF_TypeFunction:
193 case MCSA_ELF_TypeIndFunction:
194 case MCSA_ELF_TypeObject:
195 case MCSA_ELF_TypeTLS:
196 case MCSA_ELF_TypeCommon:
197 case MCSA_ELF_TypeNoType:
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000198 case MCSA_IndirectSymbol:
199 case MCSA_Hidden:
200 case MCSA_Internal:
201 case MCSA_Protected:
202 case MCSA_Weak:
203 case MCSA_Local:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000204 assert(0 && "Invalid symbol attribute for Mach-O!");
205 break;
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000206
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000207 case MCSA_Global:
Daniel Dunbar8f4d1462009-08-28 07:08:35 +0000208 SD.setExternal(true);
Daniel Dunbarb18d2dd2010-05-17 20:12:31 +0000209 // This effectively clears the undefined lazy bit, in Darwin 'as', although
210 // it isn't very consistent because it implements this as part of symbol
211 // lookup.
212 //
213 // FIXME: Cleanup this code, these bits should be emitted based on semantic
214 // properties, not on the order of definition, etc.
215 SD.setFlags(SD.getFlags() & ~SF_ReferenceTypeUndefinedLazy);
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000216 break;
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000217
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000218 case MCSA_LazyReference:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000219 // FIXME: This requires -dynamic.
220 SD.setFlags(SD.getFlags() | SF_NoDeadStrip);
221 if (Symbol->isUndefined())
222 SD.setFlags(SD.getFlags() | SF_ReferenceTypeUndefinedLazy);
223 break;
224
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000225 // Since .reference sets the no dead strip bit, it is equivalent to
226 // .no_dead_strip in practice.
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000227 case MCSA_Reference:
228 case MCSA_NoDeadStrip:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000229 SD.setFlags(SD.getFlags() | SF_NoDeadStrip);
230 break;
231
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000232 case MCSA_PrivateExtern:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000233 SD.setExternal(true);
234 SD.setPrivateExtern(true);
235 break;
236
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000237 case MCSA_WeakReference:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000238 // FIXME: This requires -dynamic.
239 if (Symbol->isUndefined())
240 SD.setFlags(SD.getFlags() | SF_WeakReference);
241 break;
242
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000243 case MCSA_WeakDefinition:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000244 // FIXME: 'as' enforces that this is defined and global. The manual claims
245 // it has to be in a coalesced section, but this isn't enforced.
246 SD.setFlags(SD.getFlags() | SF_WeakDefinition);
247 break;
Kevin Enderbyf59cac52010-07-08 17:22:42 +0000248
249 case MCSA_WeakDefAutoPrivate:
250 SD.setFlags(SD.getFlags() | SF_WeakDefinition | SF_WeakReference);
251 break;
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000252 }
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000253}
254
255void MCMachOStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000256 // Encode the 'desc' value into the lowest implementation defined bits.
Daniel Dunbar2ac0c452010-05-26 20:37:00 +0000257 assert(DescValue == (DescValue & SF_DescFlagsMask) &&
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000258 "Invalid .desc value!");
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000259 getAssembler().getOrCreateSymbolData(*Symbol).setFlags(
260 DescValue & SF_DescFlagsMask);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000261}
262
Chris Lattner9eb158d2010-01-23 07:47:02 +0000263void MCMachOStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000264 unsigned ByteAlignment) {
Daniel Dunbar8f4d1462009-08-28 07:08:35 +0000265 // FIXME: Darwin 'as' does appear to allow redef of a .comm by itself.
266 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
267
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000268 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
Daniel Dunbar8f4d1462009-08-28 07:08:35 +0000269 SD.setExternal(true);
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000270 SD.setCommon(Size, ByteAlignment);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000271}
272
Daniel Dunbar8751b942009-08-28 05:48:22 +0000273void MCMachOStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000274 unsigned Size, unsigned ByteAlignment) {
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000275 MCSectionData &SectData = getAssembler().getOrCreateSectionData(*Section);
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000276
277 // The symbol may not be present, which only creates the section.
278 if (!Symbol)
279 return;
280
281 // FIXME: Assert that this section has the zerofill type.
282
283 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
284
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000285 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000286
Daniel Dunbare73d49e2010-05-12 22:51:27 +0000287 // Emit an align fragment if necessary.
288 if (ByteAlignment != 1)
Daniel Dunbar1c154132010-05-12 22:56:23 +0000289 new MCAlignFragment(ByteAlignment, 0, 0, ByteAlignment, &SectData);
Daniel Dunbare73d49e2010-05-12 22:51:27 +0000290
Daniel Dunbar4e544872010-05-12 22:51:38 +0000291 MCFragment *F = new MCFillFragment(0, 0, Size, &SectData);
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000292 SD.setFragment(F);
293
294 Symbol->setSection(*Section);
295
296 // Update the maximum alignment on the zero fill section if necessary.
297 if (ByteAlignment > SectData.getAlignment())
298 SectData.setAlignment(ByteAlignment);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000299}
300
Eric Christopher070c1ab2010-05-21 23:03:53 +0000301// This should always be called with the thread local bss section. Like the
302// .zerofill directive this doesn't actually switch sections on us.
Eric Christopher011c3f12010-05-18 21:26:41 +0000303void MCMachOStreamer::EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
304 uint64_t Size, unsigned ByteAlignment) {
305 EmitZerofill(Section, Symbol, Size, ByteAlignment);
306 return;
Eric Christopher482eba02010-05-14 01:50:28 +0000307}
308
Chris Lattneraaec2052010-01-19 19:46:13 +0000309void MCMachOStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000310 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
311 // MCObjectStreamer.
Daniel Dunbarf70f4772010-03-22 20:35:46 +0000312 getOrCreateDataFragment()->getContents().append(Data.begin(), Data.end());
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000313}
314
Chris Lattneraaec2052010-01-19 19:46:13 +0000315void MCMachOStreamer::EmitValue(const MCExpr *Value, unsigned Size,
316 unsigned AddrSpace) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000317 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
318 // MCObjectStreamer.
Daniel Dunbarf70f4772010-03-22 20:35:46 +0000319 MCDataFragment *DF = getOrCreateDataFragment();
Daniel Dunbar45f48742010-02-13 09:28:22 +0000320
321 // Avoid fixups when possible.
322 int64_t AbsValue;
Daniel Dunbar40ebe2472010-02-22 22:08:57 +0000323 if (AddValueSymbols(Value)->EvaluateAsAbsolute(AbsValue)) {
Daniel Dunbar45f48742010-02-13 09:28:22 +0000324 // FIXME: Endianness assumption.
325 for (unsigned i = 0; i != Size; ++i)
326 DF->getContents().push_back(uint8_t(AbsValue >> (i * 8)));
327 } else {
Daniel Dunbarc90e30a2010-05-26 15:18:56 +0000328 DF->addFixup(MCFixup::Create(DF->getContents().size(),
329 AddValueSymbols(Value),
330 MCFixup::getKindForSize(Size)));
Daniel Dunbar45f48742010-02-13 09:28:22 +0000331 DF->getContents().resize(DF->getContents().size() + Size, 0);
332 }
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000333}
334
335void MCMachOStreamer::EmitValueToAlignment(unsigned ByteAlignment,
336 int64_t Value, unsigned ValueSize,
337 unsigned MaxBytesToEmit) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000338 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
339 // MCObjectStreamer.
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000340 if (MaxBytesToEmit == 0)
341 MaxBytesToEmit = ByteAlignment;
Daniel Dunbara86de102010-06-16 20:04:32 +0000342 new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit,
343 getCurrentSectionData());
Kevin Enderby6e720482010-02-23 18:26:34 +0000344
345 // Update the maximum alignment on the current section if necessary.
Daniel Dunbar83b46712010-06-16 20:04:25 +0000346 if (ByteAlignment > getCurrentSectionData()->getAlignment())
347 getCurrentSectionData()->setAlignment(ByteAlignment);
Kevin Enderby6e720482010-02-23 18:26:34 +0000348}
349
350void MCMachOStreamer::EmitCodeAlignment(unsigned ByteAlignment,
351 unsigned MaxBytesToEmit) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000352 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
353 // MCObjectStreamer.
Kevin Enderby6e720482010-02-23 18:26:34 +0000354 if (MaxBytesToEmit == 0)
355 MaxBytesToEmit = ByteAlignment;
Daniel Dunbar1c154132010-05-12 22:56:23 +0000356 MCAlignFragment *F = new MCAlignFragment(ByteAlignment, 0, 1, MaxBytesToEmit,
Daniel Dunbar83b46712010-06-16 20:04:25 +0000357 getCurrentSectionData());
Daniel Dunbar1c154132010-05-12 22:56:23 +0000358 F->setEmitNops(true);
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000359
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000360 // Update the maximum alignment on the current section if necessary.
Daniel Dunbar83b46712010-06-16 20:04:25 +0000361 if (ByteAlignment > getCurrentSectionData()->getAlignment())
362 getCurrentSectionData()->setAlignment(ByteAlignment);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000363}
364
Daniel Dunbar821e3332009-08-31 08:09:28 +0000365void MCMachOStreamer::EmitValueToOffset(const MCExpr *Offset,
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000366 unsigned char Value) {
Daniel Dunbara86de102010-06-16 20:04:32 +0000367 new MCOrgFragment(*Offset, Value, getCurrentSectionData());
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000368}
369
Daniel Dunbar2ac0c452010-05-26 20:37:00 +0000370void MCMachOStreamer::EmitInstToFragment(const MCInst &Inst) {
Daniel Dunbar83b46712010-06-16 20:04:25 +0000371 MCInstFragment *IF = new MCInstFragment(Inst, getCurrentSectionData());
Daniel Dunbar2ac0c452010-05-26 20:37:00 +0000372
373 // Add the fixups and data.
374 //
375 // FIXME: Revisit this design decision when relaxation is done, we may be
376 // able to get away with not storing any extra data in the MCInst.
377 SmallVector<MCFixup, 4> Fixups;
378 SmallString<256> Code;
379 raw_svector_ostream VecOS(Code);
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000380 getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups);
Daniel Dunbar2ac0c452010-05-26 20:37:00 +0000381 VecOS.flush();
382
383 IF->getCode() = Code;
384 IF->getFixups() = Fixups;
385}
386
387void MCMachOStreamer::EmitInstToData(const MCInst &Inst) {
388 MCDataFragment *DF = getOrCreateDataFragment();
389
390 SmallVector<MCFixup, 4> Fixups;
391 SmallString<256> Code;
392 raw_svector_ostream VecOS(Code);
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000393 getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups);
Daniel Dunbar2ac0c452010-05-26 20:37:00 +0000394 VecOS.flush();
395
396 // Add the fixups and data.
397 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
398 Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size());
399 DF->addFixup(Fixups[i]);
400 }
401 DF->getContents().append(Code.begin(), Code.end());
402}
403
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000404void MCMachOStreamer::EmitInstruction(const MCInst &Inst) {
Daniel Dunbar4fac7492009-08-27 08:17:51 +0000405 // Scan for values.
Daniel Dunbardb9014d2010-05-17 21:19:59 +0000406 for (unsigned i = Inst.getNumOperands(); i--; )
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000407 if (Inst.getOperand(i).isExpr())
408 AddValueSymbols(Inst.getOperand(i).getExpr());
Daniel Dunbar4fac7492009-08-27 08:17:51 +0000409
Daniel Dunbar83b46712010-06-16 20:04:25 +0000410 getCurrentSectionData()->setHasInstructions(true);
Daniel Dunbare1ec6172010-02-02 21:44:01 +0000411
Daniel Dunbar83194de2010-05-26 20:37:03 +0000412 // If this instruction doesn't need relaxation, just emit it as data.
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000413 if (!getAssembler().getBackend().MayNeedRelaxation(Inst)) {
Daniel Dunbar2ac0c452010-05-26 20:37:00 +0000414 EmitInstToData(Inst);
Daniel Dunbar83194de2010-05-26 20:37:03 +0000415 return;
416 }
417
418 // Otherwise, if we are relaxing everything, relax the instruction as much as
419 // possible and emit it as data.
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000420 if (getAssembler().getRelaxAll()) {
Daniel Dunbar83194de2010-05-26 20:37:03 +0000421 MCInst Relaxed;
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000422 getAssembler().getBackend().RelaxInstruction(Inst, Relaxed);
423 while (getAssembler().getBackend().MayNeedRelaxation(Relaxed))
424 getAssembler().getBackend().RelaxInstruction(Relaxed, Relaxed);
Daniel Dunbar83194de2010-05-26 20:37:03 +0000425 EmitInstToData(Relaxed);
426 return;
427 }
428
429 // Otherwise emit to a separate fragment.
430 EmitInstToFragment(Inst);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000431}
432
Kevin Enderbyb07ce602010-08-09 22:52:14 +0000433//
434// This helper routine returns an expression of End - Start + IntVal for use
435// by EmitDwarfFileTable() below.
436//
437const MCExpr * MCMachOStreamer::MakeStartMinusEndExpr(MCSymbol *Start,
438 MCSymbol *End,
439 int IntVal) {
440 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
441 const MCExpr *Res =
442 MCSymbolRefExpr::Create(End, Variant, getContext());
443 const MCExpr *RHS =
444 MCSymbolRefExpr::Create(Start, Variant, getContext());
445 const MCExpr *Res1 =
446 MCBinaryExpr::Create(MCBinaryExpr::Sub, Res, RHS,getContext());
447 const MCExpr *Res2 =
448 MCConstantExpr::Create(IntVal, getContext());
449 const MCExpr *Res3 =
450 MCBinaryExpr::Create(MCBinaryExpr::Sub, Res1, Res2, getContext());
451 return Res3;
452}
453
454//
455// This emits the Dwarf file (and eventually the line) table.
456//
457void MCMachOStreamer::EmitDwarfFileTable(void) {
458 // For now make sure we don't put out the Dwarf file table if no .file
459 // directives were seen.
460 const std::vector<MCDwarfFile *> &MCDwarfFiles =
461 getContext().getMCDwarfFiles();
462 if (MCDwarfFiles.size() == 0)
463 return;
464
465 // This is the Mach-O section, for ELF it is the .debug_line section.
466 SwitchSection(getContext().getMachOSection("__DWARF", "__debug_line",
467 MCSectionMachO::S_ATTR_DEBUG,
468 0, SectionKind::getDataRelLocal()));
469
470 // Create a symbol at the beginning of this section.
471 MCSymbol *LineStartSym = getContext().CreateTempSymbol();
472 // Set the value of the symbol, as we are at the start of the section.
473 EmitLabel(LineStartSym);
474
475 // Create a symbol for the end of the section (to be set when we get there).
476 MCSymbol *LineEndSym = getContext().CreateTempSymbol();
477
478 // The first 4 bytes is the total length of the information for this
479 // compilation unit (not including these 4 bytes for the length).
480 EmitValue(MakeStartMinusEndExpr(LineStartSym, LineEndSym, 4), 4, 0);
481
482 // Next 2 bytes is the Version, which is Dwarf 2.
483 EmitIntValue(2, 2);
484
485 // Create a symbol for the end of the prologue (to be set when we get there).
486 MCSymbol *ProEndSym = getContext().CreateTempSymbol(); // Lprologue_end
487
488 // Length of the prologue, is the next 4 bytes. Which is the start of the
489 // section to the end of the prologue. Not including the 4 bytes for the
490 // total length, the 2 bytes for the version, and these 4 bytes for the
491 // length of the prologue.
492 EmitValue(MakeStartMinusEndExpr(LineStartSym, ProEndSym, (4 + 2 + 4)), 4, 0);
493
494 // Parameters of the state machine, are next.
495 // Define the architecture-dependent minimum instruction length (in
496 // bytes). This value should be rather too small than too big. */
497 // DWARF2_LINE_MIN_INSN_LENGTH
498 EmitIntValue(1, 1);
499 // Flag that indicates the initial value of the is_stmt_start flag.
500 // DWARF2_LINE_DEFAULT_IS_STMT
501 EmitIntValue(1, 1);
502 // Minimum line offset in a special line info. opcode. This value
503 // was chosen to give a reasonable range of values. */
504 // DWARF2_LINE_BASE
505 EmitIntValue(-5, 1);
506 // Range of line offsets in a special line info. opcode.
507 // DWARF2_LINE_RANGE
508 EmitIntValue(14, 1);
509 // First special line opcode - leave room for the standard opcodes.
510 // DWARF2_LINE_OPCODE_BASE
511 EmitIntValue(13, 1);
512
513 // Standard opcode lengths
514 EmitIntValue(0, 1); // length of DW_LNS_copy
515 EmitIntValue(1, 1); // length of DW_LNS_advance_pc
516 EmitIntValue(1, 1); // length of DW_LNS_advance_line
517 EmitIntValue(1, 1); // length of DW_LNS_set_file
518 EmitIntValue(1, 1); // length of DW_LNS_set_column
519 EmitIntValue(0, 1); // length of DW_LNS_negate_stmt
520 EmitIntValue(0, 1); // length of DW_LNS_set_basic_block
521 EmitIntValue(0, 1); // length of DW_LNS_const_add_pc
522 EmitIntValue(1, 1); // length of DW_LNS_fixed_advance_pc
523 EmitIntValue(0, 1); // length of DW_LNS_set_prologue_end
524 EmitIntValue(0, 1); // length of DW_LNS_set_epilogue_begin
525 EmitIntValue(1, 1); // DW_LNS_set_isa
526
527 // Put out the directory and file tables.
528
529 // First the directory table.
530 const std::vector<StringRef> &MCDwarfDirs =
531 getContext().getMCDwarfDirs();
532 for (unsigned i = 0; i < MCDwarfDirs.size(); i++) {
533 EmitBytes(MCDwarfDirs[i], 0); // the DirectoryName
534 EmitBytes(StringRef("\0", 1), 0); // the null termination of the string
535 }
536 EmitIntValue(0, 1); // Terminate the directory list
537
538 // Second the file table.
539 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
540 EmitBytes(MCDwarfFiles[i]->getName(), 0); // FileName
541 EmitBytes(StringRef("\0", 1), 0); // the null termination of the string
542 // FIXME the Directory number should be a .uleb128 not a .byte
543 EmitIntValue(MCDwarfFiles[i]->getDirIndex(), 1);
544 EmitIntValue(0, 1); // last modification timestamp (always 0)
545 EmitIntValue(0, 1); // filesize (always 0)
546 }
547 EmitIntValue(0, 1); // Terminate the file list
548
549 // This is the end of the prologue, so set the value of the symbol at the
550 // end of the prologue (that was used in a previous expression).
551 EmitLabel(ProEndSym);
552
553 // TODO: This is the point where the line tables would be emitted.
554
555 // If there are no line tables emited then we emit:
556 // The following DW_LNE_set_address sequence to set the address to zero
557 // TODO test for 32-bit or 64-bit output
558 // This is the sequence for 32-bit code
559 EmitIntValue(0, 1);
560 EmitIntValue(5, 1);
561 EmitIntValue(2, 1);
562 EmitIntValue(0, 1);
563 EmitIntValue(0, 1);
564 EmitIntValue(0, 1);
565 EmitIntValue(0, 1);
566
567 // Lastly emit the DW_LNE_end_sequence which consists of 3 bytes '00 01 01'
568 // (00 is the code for extended opcodes, followed by a ULEB128 length of the
569 // extended opcode (01), and the DW_LNE_end_sequence (01).
570 EmitIntValue(0, 1); // DW_LNS_extended_op
571 EmitIntValue(1, 1); // ULEB128 length of the extended opcode
572 EmitIntValue(1, 1); // DW_LNE_end_sequence
573
574 // This is the end of the section, so set the value of the symbol at the end
575 // of this section (that was used in a previous expression).
576 EmitLabel(LineEndSym);
577}
578
Daniel Dunbara86de102010-06-16 20:04:32 +0000579void MCMachOStreamer::Finish() {
Kevin Enderbyb07ce602010-08-09 22:52:14 +0000580 // Dump out the dwarf file and directory tables (soon to include line table)
581 EmitDwarfFileTable();
582
Daniel Dunbara86de102010-06-16 20:04:32 +0000583 // We have to set the fragment atom associations so we can relax properly for
584 // Mach-O.
585
586 // First, scan the symbol table to build a lookup table from fragments to
587 // defining symbols.
588 DenseMap<const MCFragment*, MCSymbolData*> DefiningSymbolMap;
589 for (MCAssembler::symbol_iterator it = getAssembler().symbol_begin(),
590 ie = getAssembler().symbol_end(); it != ie; ++it) {
591 if (getAssembler().isSymbolLinkerVisible(it->getSymbol()) &&
592 it->getFragment()) {
593 // An atom defining symbol should never be internal to a fragment.
594 assert(it->getOffset() == 0 && "Invalid offset in atom defining symbol!");
595 DefiningSymbolMap[it->getFragment()] = it;
596 }
597 }
598
599 // Set the fragment atom associations by tracking the last seen atom defining
600 // symbol.
601 for (MCAssembler::iterator it = getAssembler().begin(),
602 ie = getAssembler().end(); it != ie; ++it) {
603 MCSymbolData *CurrentAtom = 0;
604 for (MCSectionData::iterator it2 = it->begin(),
605 ie2 = it->end(); it2 != ie2; ++it2) {
606 if (MCSymbolData *SD = DefiningSymbolMap.lookup(it2))
607 CurrentAtom = SD;
608 it2->setAtom(CurrentAtom);
609 }
610 }
611
612 this->MCObjectStreamer::Finish();
613}
614
Daniel Dunbar1f3e4452010-03-11 01:34:27 +0000615MCStreamer *llvm::createMachOStreamer(MCContext &Context, TargetAsmBackend &TAB,
Daniel Dunbarac2884a2010-03-25 22:49:09 +0000616 raw_ostream &OS, MCCodeEmitter *CE,
617 bool RelaxAll) {
618 MCMachOStreamer *S = new MCMachOStreamer(Context, TAB, OS, CE);
619 if (RelaxAll)
620 S->getAssembler().setRelaxAll(true);
621 return S;
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000622}