blob: 671874df2c69e054ae203a7f248e4b1de2f79d38 [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.
Kevin Enderby232ab942010-08-31 22:55:11 +000036 void MakeLineEntryForSection(const MCSection *Section);
Kevin Enderbyb07ce602010-08-09 22:52:14 +000037 const MCExpr * MakeStartMinusEndExpr(MCSymbol *Start, MCSymbol *End,
38 int IntVal);
39 void EmitDwarfFileTable(void);
Daniel Dunbar2ac0c452010-05-26 20:37:00 +000040
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000041public:
Daniel Dunbar1f3e4452010-03-11 01:34:27 +000042 MCMachOStreamer(MCContext &Context, TargetAsmBackend &TAB,
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +000043 raw_ostream &OS, MCCodeEmitter *Emitter)
Daniel Dunbar83b46712010-06-16 20:04:25 +000044 : MCObjectStreamer(Context, TAB, OS, Emitter) {}
Daniel Dunbarac2884a2010-03-25 22:49:09 +000045
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000046 /// @name MCStreamer Interface
47 /// @{
48
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000049 virtual void EmitLabel(MCSymbol *Symbol);
Chris Lattnera5ad93a2010-01-23 06:39:22 +000050 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag);
Daniel Dunbar821e3332009-08-31 08:09:28 +000051 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value);
Chris Lattnera5ad93a2010-01-23 06:39:22 +000052 virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000053 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
Chris Lattner9eb158d2010-01-23 07:47:02 +000054 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +000055 unsigned ByteAlignment);
Chris Lattnerb54b9dd2010-05-08 19:54:22 +000056 virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol) {
57 assert(0 && "macho doesn't support this directive");
58 }
59 virtual void EmitCOFFSymbolStorageClass(int StorageClass) {
60 assert(0 && "macho doesn't support this directive");
61 }
62 virtual void EmitCOFFSymbolType(int Type) {
63 assert(0 && "macho doesn't support this directive");
64 }
65 virtual void EndCOFFSymbolDef() {
66 assert(0 && "macho doesn't support this directive");
67 }
Chris Lattner99328ad2010-01-25 07:52:13 +000068 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
69 assert(0 && "macho doesn't support this directive");
70 }
Chris Lattner9eb158d2010-01-23 07:47:02 +000071 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size) {
72 assert(0 && "macho doesn't support this directive");
73 }
Daniel Dunbar8751b942009-08-28 05:48:22 +000074 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +000075 unsigned Size = 0, unsigned ByteAlignment = 0);
Eric Christopher011c3f12010-05-18 21:26:41 +000076 virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
77 uint64_t Size, unsigned ByteAlignment = 0);
Chris Lattneraaec2052010-01-19 19:46:13 +000078 virtual void EmitBytes(StringRef Data, unsigned AddrSpace);
Chris Lattneraaec2052010-01-19 19:46:13 +000079 virtual void EmitValue(const MCExpr *Value, unsigned Size,unsigned AddrSpace);
Chris Lattner718fb592010-01-25 21:28:50 +000080 virtual void EmitGPRel32Value(const MCExpr *Value) {
81 assert(0 && "macho doesn't support this directive");
82 }
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000083 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
84 unsigned ValueSize = 1,
85 unsigned MaxBytesToEmit = 0);
Kevin Enderby6e720482010-02-23 18:26:34 +000086 virtual void EmitCodeAlignment(unsigned ByteAlignment,
87 unsigned MaxBytesToEmit = 0);
Daniel Dunbar821e3332009-08-31 08:09:28 +000088 virtual void EmitValueToOffset(const MCExpr *Offset,
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000089 unsigned char Value = 0);
Daniel Dunbar2ac0c452010-05-26 20:37:00 +000090
Chris Lattnera6594fc2010-01-25 18:58:59 +000091 virtual void EmitFileDirective(StringRef Filename) {
Daniel Dunbar96749672010-07-19 20:44:20 +000092 // FIXME: Just ignore the .file; it isn't important enough to fail the
93 // entire assembly.
94
95 //report_fatal_error("unsupported directive: '.file'");
Chris Lattnera6594fc2010-01-25 18:58:59 +000096 }
97 virtual void EmitDwarfFileDirective(unsigned FileNo, StringRef Filename) {
Daniel Dunbar96749672010-07-19 20:44:20 +000098 // FIXME: Just ignore the .file; it isn't important enough to fail the
99 // entire assembly.
100
101 //report_fatal_error("unsupported directive: '.file'");
Chris Lattnera6594fc2010-01-25 18:58:59 +0000102 }
Daniel Dunbar2ac0c452010-05-26 20:37:00 +0000103
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000104 virtual void EmitInstruction(const MCInst &Inst);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000105
Daniel Dunbara86de102010-06-16 20:04:32 +0000106 virtual void Finish();
107
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000108 /// @}
109};
110
111} // end anonymous namespace.
112
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000113void MCMachOStreamer::EmitLabel(MCSymbol *Symbol) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000114 // TODO: This is almost exactly the same as WinCOFFStreamer. Consider merging
115 // into MCObjectStreamer.
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000116 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
Daniel Dunbarc3047182010-05-05 19:01:00 +0000117 assert(!Symbol->isVariable() && "Cannot emit a variable symbol!");
118 assert(CurSection && "Cannot emit before setting section!");
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000119
Daniel Dunbara86de102010-06-16 20:04:32 +0000120 Symbol->setSection(*CurSection);
121
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000122 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
Daniel Dunbar071f73d2010-05-10 22:45:09 +0000123
Daniel Dunbara86de102010-06-16 20:04:32 +0000124 // We have to create a new fragment if this is an atom defining symbol,
125 // fragments cannot span atoms.
126 if (getAssembler().isSymbolLinkerVisible(SD.getSymbol()))
127 new MCDataFragment(getCurrentSectionData());
Daniel Dunbar071f73d2010-05-10 22:45:09 +0000128
Daniel Dunbarf70f4772010-03-22 20:35:46 +0000129 // FIXME: This is wasteful, we don't necessarily need to create a data
130 // fragment. Instead, we should mark the symbol as pointing into the data
131 // fragment if it exists, otherwise we should just queue the label and set its
132 // fragment pointer when we emit the next fragment.
Daniel Dunbara86de102010-06-16 20:04:32 +0000133 MCDataFragment *F = getOrCreateDataFragment();
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000134 assert(!SD.getFragment() && "Unexpected fragment on symbol data!");
135 SD.setFragment(F);
136 SD.setOffset(F->getContents().size());
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000137
Daniel Dunbarb18d2dd2010-05-17 20:12:31 +0000138 // This causes the reference type flag to be cleared. Darwin 'as' was "trying"
139 // to clear the weak reference and weak definition bits too, but the
140 // implementation was buggy. For now we just try to match 'as', for
141 // diffability.
142 //
143 // FIXME: Cleanup this code, these bits should be emitted based on semantic
144 // properties, not on the order of definition, etc.
145 SD.setFlags(SD.getFlags() & ~SF_ReferenceTypeMask);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000146}
147
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000148void MCMachOStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
Daniel Dunbar6009db42009-08-26 21:22:22 +0000149 switch (Flag) {
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000150 case MCAF_SubsectionsViaSymbols:
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000151 getAssembler().setSubsectionsViaSymbols(true);
Daniel Dunbar8c3eaf42009-08-28 07:08:47 +0000152 return;
Daniel Dunbar6009db42009-08-26 21:22:22 +0000153 }
Daniel Dunbar8c3eaf42009-08-28 07:08:47 +0000154
155 assert(0 && "invalid assembler flag!");
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000156}
157
Daniel Dunbar821e3332009-08-31 08:09:28 +0000158void MCMachOStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000159 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
160 // MCObjectStreamer.
Daniel Dunbara4d86672009-10-16 01:58:23 +0000161 // FIXME: Lift context changes into super class.
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000162 getAssembler().getOrCreateSymbolData(*Symbol);
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000163 Symbol->setVariableValue(AddValueSymbols(Value));
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000164}
165
166void MCMachOStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000167 MCSymbolAttr Attribute) {
Daniel Dunbar0c7761b2009-08-24 11:56:58 +0000168 // Indirect symbols are handled differently, to match how 'as' handles
169 // them. This makes writing matching .o files easier.
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000170 if (Attribute == MCSA_IndirectSymbol) {
Daniel Dunbarad7c3d52009-08-26 00:18:21 +0000171 // Note that we intentionally cannot use the symbol data here; this is
172 // important for matching the string table that 'as' generates.
Daniel Dunbar0c7761b2009-08-24 11:56:58 +0000173 IndirectSymbolData ISD;
174 ISD.Symbol = Symbol;
Daniel Dunbar83b46712010-06-16 20:04:25 +0000175 ISD.SectionData = getCurrentSectionData();
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000176 getAssembler().getIndirectSymbols().push_back(ISD);
Daniel Dunbar0c7761b2009-08-24 11:56:58 +0000177 return;
178 }
179
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000180 // Adding a symbol attribute always introduces the symbol, note that an
Daniel Dunbar46836a72010-03-10 20:58:29 +0000181 // important side effect of calling getOrCreateSymbolData here is to register
182 // the symbol with the assembler.
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000183 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000184
185 // The implementation of symbol attributes is designed to match 'as', but it
186 // leaves much to desired. It doesn't really make sense to arbitrarily add and
187 // remove flags, but 'as' allows this (in particular, see .desc).
188 //
189 // In the future it might be worth trying to make these operations more well
190 // defined.
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000191 switch (Attribute) {
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000192 case MCSA_Invalid:
Chris Lattnered0ab152010-01-25 18:30:45 +0000193 case MCSA_ELF_TypeFunction:
194 case MCSA_ELF_TypeIndFunction:
195 case MCSA_ELF_TypeObject:
196 case MCSA_ELF_TypeTLS:
197 case MCSA_ELF_TypeCommon:
198 case MCSA_ELF_TypeNoType:
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000199 case MCSA_IndirectSymbol:
200 case MCSA_Hidden:
201 case MCSA_Internal:
202 case MCSA_Protected:
203 case MCSA_Weak:
204 case MCSA_Local:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000205 assert(0 && "Invalid symbol attribute for Mach-O!");
206 break;
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000207
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000208 case MCSA_Global:
Daniel Dunbar8f4d1462009-08-28 07:08:35 +0000209 SD.setExternal(true);
Daniel Dunbarb18d2dd2010-05-17 20:12:31 +0000210 // This effectively clears the undefined lazy bit, in Darwin 'as', although
211 // it isn't very consistent because it implements this as part of symbol
212 // lookup.
213 //
214 // FIXME: Cleanup this code, these bits should be emitted based on semantic
215 // properties, not on the order of definition, etc.
216 SD.setFlags(SD.getFlags() & ~SF_ReferenceTypeUndefinedLazy);
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000217 break;
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000218
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000219 case MCSA_LazyReference:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000220 // FIXME: This requires -dynamic.
221 SD.setFlags(SD.getFlags() | SF_NoDeadStrip);
222 if (Symbol->isUndefined())
223 SD.setFlags(SD.getFlags() | SF_ReferenceTypeUndefinedLazy);
224 break;
225
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000226 // Since .reference sets the no dead strip bit, it is equivalent to
227 // .no_dead_strip in practice.
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000228 case MCSA_Reference:
229 case MCSA_NoDeadStrip:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000230 SD.setFlags(SD.getFlags() | SF_NoDeadStrip);
231 break;
232
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000233 case MCSA_PrivateExtern:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000234 SD.setExternal(true);
235 SD.setPrivateExtern(true);
236 break;
237
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000238 case MCSA_WeakReference:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000239 // FIXME: This requires -dynamic.
240 if (Symbol->isUndefined())
241 SD.setFlags(SD.getFlags() | SF_WeakReference);
242 break;
243
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000244 case MCSA_WeakDefinition:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000245 // FIXME: 'as' enforces that this is defined and global. The manual claims
246 // it has to be in a coalesced section, but this isn't enforced.
247 SD.setFlags(SD.getFlags() | SF_WeakDefinition);
248 break;
Kevin Enderbyf59cac52010-07-08 17:22:42 +0000249
250 case MCSA_WeakDefAutoPrivate:
251 SD.setFlags(SD.getFlags() | SF_WeakDefinition | SF_WeakReference);
252 break;
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000253 }
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000254}
255
256void MCMachOStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000257 // Encode the 'desc' value into the lowest implementation defined bits.
Daniel Dunbar2ac0c452010-05-26 20:37:00 +0000258 assert(DescValue == (DescValue & SF_DescFlagsMask) &&
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000259 "Invalid .desc value!");
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000260 getAssembler().getOrCreateSymbolData(*Symbol).setFlags(
261 DescValue & SF_DescFlagsMask);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000262}
263
Chris Lattner9eb158d2010-01-23 07:47:02 +0000264void MCMachOStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000265 unsigned ByteAlignment) {
Daniel Dunbar8f4d1462009-08-28 07:08:35 +0000266 // FIXME: Darwin 'as' does appear to allow redef of a .comm by itself.
267 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
268
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000269 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
Daniel Dunbar8f4d1462009-08-28 07:08:35 +0000270 SD.setExternal(true);
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000271 SD.setCommon(Size, ByteAlignment);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000272}
273
Daniel Dunbar8751b942009-08-28 05:48:22 +0000274void MCMachOStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000275 unsigned Size, unsigned ByteAlignment) {
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000276 MCSectionData &SectData = getAssembler().getOrCreateSectionData(*Section);
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000277
278 // The symbol may not be present, which only creates the section.
279 if (!Symbol)
280 return;
281
282 // FIXME: Assert that this section has the zerofill type.
283
284 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
285
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000286 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000287
Daniel Dunbare73d49e2010-05-12 22:51:27 +0000288 // Emit an align fragment if necessary.
289 if (ByteAlignment != 1)
Daniel Dunbar1c154132010-05-12 22:56:23 +0000290 new MCAlignFragment(ByteAlignment, 0, 0, ByteAlignment, &SectData);
Daniel Dunbare73d49e2010-05-12 22:51:27 +0000291
Daniel Dunbar4e544872010-05-12 22:51:38 +0000292 MCFragment *F = new MCFillFragment(0, 0, Size, &SectData);
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000293 SD.setFragment(F);
294
295 Symbol->setSection(*Section);
296
297 // Update the maximum alignment on the zero fill section if necessary.
298 if (ByteAlignment > SectData.getAlignment())
299 SectData.setAlignment(ByteAlignment);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000300}
301
Eric Christopher070c1ab2010-05-21 23:03:53 +0000302// This should always be called with the thread local bss section. Like the
303// .zerofill directive this doesn't actually switch sections on us.
Eric Christopher011c3f12010-05-18 21:26:41 +0000304void MCMachOStreamer::EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
305 uint64_t Size, unsigned ByteAlignment) {
306 EmitZerofill(Section, Symbol, Size, ByteAlignment);
307 return;
Eric Christopher482eba02010-05-14 01:50:28 +0000308}
309
Chris Lattneraaec2052010-01-19 19:46:13 +0000310void MCMachOStreamer::EmitBytes(StringRef Data, 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 getOrCreateDataFragment()->getContents().append(Data.begin(), Data.end());
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000314}
315
Chris Lattneraaec2052010-01-19 19:46:13 +0000316void MCMachOStreamer::EmitValue(const MCExpr *Value, unsigned Size,
317 unsigned AddrSpace) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000318 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
319 // MCObjectStreamer.
Daniel Dunbarf70f4772010-03-22 20:35:46 +0000320 MCDataFragment *DF = getOrCreateDataFragment();
Daniel Dunbar45f48742010-02-13 09:28:22 +0000321
322 // Avoid fixups when possible.
323 int64_t AbsValue;
Daniel Dunbar40ebe2472010-02-22 22:08:57 +0000324 if (AddValueSymbols(Value)->EvaluateAsAbsolute(AbsValue)) {
Daniel Dunbar45f48742010-02-13 09:28:22 +0000325 // FIXME: Endianness assumption.
326 for (unsigned i = 0; i != Size; ++i)
327 DF->getContents().push_back(uint8_t(AbsValue >> (i * 8)));
328 } else {
Daniel Dunbarc90e30a2010-05-26 15:18:56 +0000329 DF->addFixup(MCFixup::Create(DF->getContents().size(),
330 AddValueSymbols(Value),
331 MCFixup::getKindForSize(Size)));
Daniel Dunbar45f48742010-02-13 09:28:22 +0000332 DF->getContents().resize(DF->getContents().size() + Size, 0);
333 }
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000334}
335
336void MCMachOStreamer::EmitValueToAlignment(unsigned ByteAlignment,
337 int64_t Value, unsigned ValueSize,
338 unsigned MaxBytesToEmit) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000339 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
340 // MCObjectStreamer.
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000341 if (MaxBytesToEmit == 0)
342 MaxBytesToEmit = ByteAlignment;
Daniel Dunbara86de102010-06-16 20:04:32 +0000343 new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit,
344 getCurrentSectionData());
Kevin Enderby6e720482010-02-23 18:26:34 +0000345
346 // Update the maximum alignment on the current section if necessary.
Daniel Dunbar83b46712010-06-16 20:04:25 +0000347 if (ByteAlignment > getCurrentSectionData()->getAlignment())
348 getCurrentSectionData()->setAlignment(ByteAlignment);
Kevin Enderby6e720482010-02-23 18:26:34 +0000349}
350
351void MCMachOStreamer::EmitCodeAlignment(unsigned ByteAlignment,
352 unsigned MaxBytesToEmit) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000353 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
354 // MCObjectStreamer.
Kevin Enderby6e720482010-02-23 18:26:34 +0000355 if (MaxBytesToEmit == 0)
356 MaxBytesToEmit = ByteAlignment;
Daniel Dunbar1c154132010-05-12 22:56:23 +0000357 MCAlignFragment *F = new MCAlignFragment(ByteAlignment, 0, 1, MaxBytesToEmit,
Daniel Dunbar83b46712010-06-16 20:04:25 +0000358 getCurrentSectionData());
Daniel Dunbar1c154132010-05-12 22:56:23 +0000359 F->setEmitNops(true);
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000360
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000361 // Update the maximum alignment on the current section if necessary.
Daniel Dunbar83b46712010-06-16 20:04:25 +0000362 if (ByteAlignment > getCurrentSectionData()->getAlignment())
363 getCurrentSectionData()->setAlignment(ByteAlignment);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000364}
365
Daniel Dunbar821e3332009-08-31 08:09:28 +0000366void MCMachOStreamer::EmitValueToOffset(const MCExpr *Offset,
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000367 unsigned char Value) {
Daniel Dunbara86de102010-06-16 20:04:32 +0000368 new MCOrgFragment(*Offset, Value, getCurrentSectionData());
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000369}
370
Daniel Dunbar2ac0c452010-05-26 20:37:00 +0000371void MCMachOStreamer::EmitInstToFragment(const MCInst &Inst) {
Daniel Dunbar83b46712010-06-16 20:04:25 +0000372 MCInstFragment *IF = new MCInstFragment(Inst, getCurrentSectionData());
Daniel Dunbar2ac0c452010-05-26 20:37:00 +0000373
374 // Add the fixups and data.
375 //
376 // FIXME: Revisit this design decision when relaxation is done, we may be
377 // able to get away with not storing any extra data in the MCInst.
378 SmallVector<MCFixup, 4> Fixups;
379 SmallString<256> Code;
380 raw_svector_ostream VecOS(Code);
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000381 getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups);
Daniel Dunbar2ac0c452010-05-26 20:37:00 +0000382 VecOS.flush();
383
384 IF->getCode() = Code;
385 IF->getFixups() = Fixups;
386}
387
388void MCMachOStreamer::EmitInstToData(const MCInst &Inst) {
389 MCDataFragment *DF = getOrCreateDataFragment();
390
391 SmallVector<MCFixup, 4> Fixups;
392 SmallString<256> Code;
393 raw_svector_ostream VecOS(Code);
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000394 getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups);
Daniel Dunbar2ac0c452010-05-26 20:37:00 +0000395 VecOS.flush();
396
397 // Add the fixups and data.
398 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
399 Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size());
400 DF->addFixup(Fixups[i]);
401 }
402 DF->getContents().append(Code.begin(), Code.end());
403}
404
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000405void MCMachOStreamer::EmitInstruction(const MCInst &Inst) {
Daniel Dunbar4fac7492009-08-27 08:17:51 +0000406 // Scan for values.
Daniel Dunbardb9014d2010-05-17 21:19:59 +0000407 for (unsigned i = Inst.getNumOperands(); i--; )
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000408 if (Inst.getOperand(i).isExpr())
409 AddValueSymbols(Inst.getOperand(i).getExpr());
Daniel Dunbar4fac7492009-08-27 08:17:51 +0000410
Daniel Dunbar83b46712010-06-16 20:04:25 +0000411 getCurrentSectionData()->setHasInstructions(true);
Daniel Dunbare1ec6172010-02-02 21:44:01 +0000412
Kevin Enderby232ab942010-08-31 22:55:11 +0000413 // Now that a machine instruction has been assembled into this section, make
414 // a line entry for any .loc directive that has been seen.
415 MakeLineEntryForSection(getCurrentSection());
416
Daniel Dunbar83194de2010-05-26 20:37:03 +0000417 // If this instruction doesn't need relaxation, just emit it as data.
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000418 if (!getAssembler().getBackend().MayNeedRelaxation(Inst)) {
Daniel Dunbar2ac0c452010-05-26 20:37:00 +0000419 EmitInstToData(Inst);
Daniel Dunbar83194de2010-05-26 20:37:03 +0000420 return;
421 }
422
423 // Otherwise, if we are relaxing everything, relax the instruction as much as
424 // possible and emit it as data.
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000425 if (getAssembler().getRelaxAll()) {
Daniel Dunbar83194de2010-05-26 20:37:03 +0000426 MCInst Relaxed;
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000427 getAssembler().getBackend().RelaxInstruction(Inst, Relaxed);
428 while (getAssembler().getBackend().MayNeedRelaxation(Relaxed))
429 getAssembler().getBackend().RelaxInstruction(Relaxed, Relaxed);
Daniel Dunbar83194de2010-05-26 20:37:03 +0000430 EmitInstToData(Relaxed);
431 return;
432 }
433
434 // Otherwise emit to a separate fragment.
435 EmitInstToFragment(Inst);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000436}
437
Kevin Enderbyb07ce602010-08-09 22:52:14 +0000438//
Kevin Enderby232ab942010-08-31 22:55:11 +0000439// This is called when an instruction is assembled into the specified section
440// and if there is information from the last .loc directive that has yet to have
441// a line entry made for it is made.
442//
443void MCMachOStreamer::MakeLineEntryForSection(const MCSection *Section) {
444 if (!getContext().getDwarfLocSeen())
445 return;
446
447 // Create a symbol at in the current section for use in the line entry.
448 MCSymbol *LineSym = getContext().CreateTempSymbol();
449 // Set the value of the symbol to use for the MCLineEntry.
450 EmitLabel(LineSym);
451
452 // Get the current .loc info saved in the context.
453 const MCDwarfLoc &DwarfLoc = getContext().getCurrentDwarfLoc();
454
455 // Create a (local) line entry with the symbol and the current .loc info.
456 MCLineEntry LineEntry(LineSym, DwarfLoc);
457
458 // clear DwarfLocSeen saying the current .loc info is now used.
459 getContext().clearDwarfLocSeen();
460
461 // Get the MCLineSection for this section, if one does not exist for this
462 // section create it.
463 DenseMap<const MCSection *, MCLineSection *> &MCLineSections =
464 getContext().getMCLineSections();
465 MCLineSection *LineSection = MCLineSections[Section];
466 if (!LineSection) {
467 // Create a new MCLineSection. This will be deleted after the dwarf line
468 // table is created using it by iterating through the MCLineSections
469 // DenseMap.
470 LineSection = new MCLineSection;
471 // Save a pointer to the new LineSection into the MCLineSections DenseMap.
472 MCLineSections[Section] = LineSection;
473 }
474
475 // Add the line entry to this section's entries.
476 LineSection->addLineEntry(LineEntry);
477}
478
479//
Kevin Enderbyb07ce602010-08-09 22:52:14 +0000480// This helper routine returns an expression of End - Start + IntVal for use
481// by EmitDwarfFileTable() below.
482//
483const MCExpr * MCMachOStreamer::MakeStartMinusEndExpr(MCSymbol *Start,
484 MCSymbol *End,
485 int IntVal) {
486 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
487 const MCExpr *Res =
488 MCSymbolRefExpr::Create(End, Variant, getContext());
489 const MCExpr *RHS =
490 MCSymbolRefExpr::Create(Start, Variant, getContext());
491 const MCExpr *Res1 =
492 MCBinaryExpr::Create(MCBinaryExpr::Sub, Res, RHS,getContext());
493 const MCExpr *Res2 =
494 MCConstantExpr::Create(IntVal, getContext());
495 const MCExpr *Res3 =
496 MCBinaryExpr::Create(MCBinaryExpr::Sub, Res1, Res2, getContext());
497 return Res3;
498}
499
500//
501// This emits the Dwarf file (and eventually the line) table.
502//
503void MCMachOStreamer::EmitDwarfFileTable(void) {
504 // For now make sure we don't put out the Dwarf file table if no .file
505 // directives were seen.
506 const std::vector<MCDwarfFile *> &MCDwarfFiles =
507 getContext().getMCDwarfFiles();
508 if (MCDwarfFiles.size() == 0)
509 return;
510
511 // This is the Mach-O section, for ELF it is the .debug_line section.
512 SwitchSection(getContext().getMachOSection("__DWARF", "__debug_line",
513 MCSectionMachO::S_ATTR_DEBUG,
514 0, SectionKind::getDataRelLocal()));
515
516 // Create a symbol at the beginning of this section.
517 MCSymbol *LineStartSym = getContext().CreateTempSymbol();
518 // Set the value of the symbol, as we are at the start of the section.
519 EmitLabel(LineStartSym);
520
521 // Create a symbol for the end of the section (to be set when we get there).
522 MCSymbol *LineEndSym = getContext().CreateTempSymbol();
523
524 // The first 4 bytes is the total length of the information for this
525 // compilation unit (not including these 4 bytes for the length).
526 EmitValue(MakeStartMinusEndExpr(LineStartSym, LineEndSym, 4), 4, 0);
527
528 // Next 2 bytes is the Version, which is Dwarf 2.
529 EmitIntValue(2, 2);
530
531 // Create a symbol for the end of the prologue (to be set when we get there).
532 MCSymbol *ProEndSym = getContext().CreateTempSymbol(); // Lprologue_end
533
534 // Length of the prologue, is the next 4 bytes. Which is the start of the
535 // section to the end of the prologue. Not including the 4 bytes for the
536 // total length, the 2 bytes for the version, and these 4 bytes for the
537 // length of the prologue.
538 EmitValue(MakeStartMinusEndExpr(LineStartSym, ProEndSym, (4 + 2 + 4)), 4, 0);
539
540 // Parameters of the state machine, are next.
541 // Define the architecture-dependent minimum instruction length (in
542 // bytes). This value should be rather too small than too big. */
543 // DWARF2_LINE_MIN_INSN_LENGTH
544 EmitIntValue(1, 1);
545 // Flag that indicates the initial value of the is_stmt_start flag.
546 // DWARF2_LINE_DEFAULT_IS_STMT
547 EmitIntValue(1, 1);
548 // Minimum line offset in a special line info. opcode. This value
549 // was chosen to give a reasonable range of values. */
550 // DWARF2_LINE_BASE
Bill Wendling7d49f2b2010-09-03 19:09:46 +0000551 EmitIntValue(uint64_t(-5), 1);
Kevin Enderbyb07ce602010-08-09 22:52:14 +0000552 // Range of line offsets in a special line info. opcode.
553 // DWARF2_LINE_RANGE
554 EmitIntValue(14, 1);
555 // First special line opcode - leave room for the standard opcodes.
556 // DWARF2_LINE_OPCODE_BASE
557 EmitIntValue(13, 1);
558
559 // Standard opcode lengths
560 EmitIntValue(0, 1); // length of DW_LNS_copy
561 EmitIntValue(1, 1); // length of DW_LNS_advance_pc
562 EmitIntValue(1, 1); // length of DW_LNS_advance_line
563 EmitIntValue(1, 1); // length of DW_LNS_set_file
564 EmitIntValue(1, 1); // length of DW_LNS_set_column
565 EmitIntValue(0, 1); // length of DW_LNS_negate_stmt
566 EmitIntValue(0, 1); // length of DW_LNS_set_basic_block
567 EmitIntValue(0, 1); // length of DW_LNS_const_add_pc
568 EmitIntValue(1, 1); // length of DW_LNS_fixed_advance_pc
569 EmitIntValue(0, 1); // length of DW_LNS_set_prologue_end
570 EmitIntValue(0, 1); // length of DW_LNS_set_epilogue_begin
571 EmitIntValue(1, 1); // DW_LNS_set_isa
572
573 // Put out the directory and file tables.
574
575 // First the directory table.
576 const std::vector<StringRef> &MCDwarfDirs =
577 getContext().getMCDwarfDirs();
578 for (unsigned i = 0; i < MCDwarfDirs.size(); i++) {
579 EmitBytes(MCDwarfDirs[i], 0); // the DirectoryName
580 EmitBytes(StringRef("\0", 1), 0); // the null termination of the string
581 }
582 EmitIntValue(0, 1); // Terminate the directory list
583
584 // Second the file table.
585 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
586 EmitBytes(MCDwarfFiles[i]->getName(), 0); // FileName
587 EmitBytes(StringRef("\0", 1), 0); // the null termination of the string
588 // FIXME the Directory number should be a .uleb128 not a .byte
589 EmitIntValue(MCDwarfFiles[i]->getDirIndex(), 1);
590 EmitIntValue(0, 1); // last modification timestamp (always 0)
591 EmitIntValue(0, 1); // filesize (always 0)
592 }
593 EmitIntValue(0, 1); // Terminate the file list
594
595 // This is the end of the prologue, so set the value of the symbol at the
596 // end of the prologue (that was used in a previous expression).
597 EmitLabel(ProEndSym);
598
599 // TODO: This is the point where the line tables would be emitted.
600
Kevin Enderby232ab942010-08-31 22:55:11 +0000601 // Delete the MCLineSections that were created in
602 // MCMachOStreamer::MakeLineEntryForSection() and used to emit the line
603 // tables.
604 DenseMap<const MCSection *, MCLineSection *> &MCLineSections =
605 getContext().getMCLineSections();
606 for (DenseMap<const MCSection *, MCLineSection *>::iterator it =
607 MCLineSections.begin(), ie = MCLineSections.end(); it != ie; ++it) {
608 delete it->second;
609 }
610
Kevin Enderbyb07ce602010-08-09 22:52:14 +0000611 // If there are no line tables emited then we emit:
612 // The following DW_LNE_set_address sequence to set the address to zero
613 // TODO test for 32-bit or 64-bit output
614 // This is the sequence for 32-bit code
615 EmitIntValue(0, 1);
616 EmitIntValue(5, 1);
617 EmitIntValue(2, 1);
618 EmitIntValue(0, 1);
619 EmitIntValue(0, 1);
620 EmitIntValue(0, 1);
621 EmitIntValue(0, 1);
622
623 // Lastly emit the DW_LNE_end_sequence which consists of 3 bytes '00 01 01'
624 // (00 is the code for extended opcodes, followed by a ULEB128 length of the
625 // extended opcode (01), and the DW_LNE_end_sequence (01).
626 EmitIntValue(0, 1); // DW_LNS_extended_op
627 EmitIntValue(1, 1); // ULEB128 length of the extended opcode
628 EmitIntValue(1, 1); // DW_LNE_end_sequence
629
630 // This is the end of the section, so set the value of the symbol at the end
631 // of this section (that was used in a previous expression).
632 EmitLabel(LineEndSym);
633}
634
Daniel Dunbara86de102010-06-16 20:04:32 +0000635void MCMachOStreamer::Finish() {
Kevin Enderbyb07ce602010-08-09 22:52:14 +0000636 // Dump out the dwarf file and directory tables (soon to include line table)
637 EmitDwarfFileTable();
638
Daniel Dunbara86de102010-06-16 20:04:32 +0000639 // We have to set the fragment atom associations so we can relax properly for
640 // Mach-O.
641
642 // First, scan the symbol table to build a lookup table from fragments to
643 // defining symbols.
644 DenseMap<const MCFragment*, MCSymbolData*> DefiningSymbolMap;
645 for (MCAssembler::symbol_iterator it = getAssembler().symbol_begin(),
646 ie = getAssembler().symbol_end(); it != ie; ++it) {
647 if (getAssembler().isSymbolLinkerVisible(it->getSymbol()) &&
648 it->getFragment()) {
649 // An atom defining symbol should never be internal to a fragment.
650 assert(it->getOffset() == 0 && "Invalid offset in atom defining symbol!");
651 DefiningSymbolMap[it->getFragment()] = it;
652 }
653 }
654
655 // Set the fragment atom associations by tracking the last seen atom defining
656 // symbol.
657 for (MCAssembler::iterator it = getAssembler().begin(),
658 ie = getAssembler().end(); it != ie; ++it) {
659 MCSymbolData *CurrentAtom = 0;
660 for (MCSectionData::iterator it2 = it->begin(),
661 ie2 = it->end(); it2 != ie2; ++it2) {
662 if (MCSymbolData *SD = DefiningSymbolMap.lookup(it2))
663 CurrentAtom = SD;
664 it2->setAtom(CurrentAtom);
665 }
666 }
667
668 this->MCObjectStreamer::Finish();
669}
670
Daniel Dunbar1f3e4452010-03-11 01:34:27 +0000671MCStreamer *llvm::createMachOStreamer(MCContext &Context, TargetAsmBackend &TAB,
Daniel Dunbarac2884a2010-03-25 22:49:09 +0000672 raw_ostream &OS, MCCodeEmitter *CE,
673 bool RelaxAll) {
674 MCMachOStreamer *S = new MCMachOStreamer(Context, TAB, OS, CE);
675 if (RelaxAll)
676 S->getAssembler().setRelaxAll(true);
677 return S;
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000678}