blob: b27f151f4341b0be6443b3bae9e449314530c591 [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 Dunbarfb4a6b32009-08-21 09:11:24 +000017#include "llvm/MC/MCSection.h"
18#include "llvm/MC/MCSymbol.h"
Kevin Enderbya6eeb6e2010-05-07 21:44:23 +000019#include "llvm/MC/MCMachOSymbolFlags.h"
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000020#include "llvm/Support/ErrorHandling.h"
Daniel Dunbar4fac7492009-08-27 08:17:51 +000021#include "llvm/Support/raw_ostream.h"
Daniel Dunbard8036fb2010-03-23 05:09:03 +000022#include "llvm/Target/TargetAsmBackend.h"
23
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000024using namespace llvm;
25
26namespace {
27
28class MCMachOStreamer : public MCStreamer {
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +000029
30private:
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000031 MCAssembler Assembler;
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000032 MCSectionData *CurSectionData;
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +000033
Daniel Dunbar071f73d2010-05-10 22:45:09 +000034 /// Track the current atom for each section.
35 DenseMap<const MCSectionData*, MCSymbolData*> CurrentAtomMap;
36
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +000037private:
38 MCFragment *getCurrentFragment() const {
39 assert(CurSectionData && "No current section!");
40
41 if (!CurSectionData->empty())
42 return &CurSectionData->getFragmentList().back();
43
44 return 0;
45 }
46
Daniel Dunbarf70f4772010-03-22 20:35:46 +000047 /// Get a data fragment to write into, creating a new one if the current
48 /// fragment is not a data fragment.
49 MCDataFragment *getOrCreateDataFragment() const {
50 MCDataFragment *F = dyn_cast_or_null<MCDataFragment>(getCurrentFragment());
51 if (!F)
Daniel Dunbar071f73d2010-05-10 22:45:09 +000052 F = createDataFragment();
Daniel Dunbarf70f4772010-03-22 20:35:46 +000053 return F;
54 }
55
Daniel Dunbar071f73d2010-05-10 22:45:09 +000056 /// Create a new data fragment in the current section.
57 MCDataFragment *createDataFragment() const {
58 MCDataFragment *DF = new MCDataFragment(CurSectionData);
59 DF->setAtom(CurrentAtomMap.lookup(CurSectionData));
60 return DF;
61 }
62
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000063public:
Daniel Dunbar1f3e4452010-03-11 01:34:27 +000064 MCMachOStreamer(MCContext &Context, TargetAsmBackend &TAB,
65 raw_ostream &_OS, MCCodeEmitter *_Emitter)
Daniel Dunbarcf871e52010-03-19 10:43:18 +000066 : MCStreamer(Context), Assembler(Context, TAB, *_Emitter, _OS),
Daniel Dunbar4fac7492009-08-27 08:17:51 +000067 CurSectionData(0) {}
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000068 ~MCMachOStreamer() {}
69
Daniel Dunbarac2884a2010-03-25 22:49:09 +000070 MCAssembler &getAssembler() { return Assembler; }
71
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +000072 const MCExpr *AddValueSymbols(const MCExpr *Value) {
73 switch (Value->getKind()) {
Chris Lattner5d917a82010-02-08 19:41:07 +000074 case MCExpr::Target: assert(0 && "Can't handle target exprs yet!");
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +000075 case MCExpr::Constant:
76 break;
77
78 case MCExpr::Binary: {
79 const MCBinaryExpr *BE = cast<MCBinaryExpr>(Value);
80 AddValueSymbols(BE->getLHS());
81 AddValueSymbols(BE->getRHS());
82 break;
83 }
84
85 case MCExpr::SymbolRef:
Daniel Dunbar46836a72010-03-10 20:58:29 +000086 Assembler.getOrCreateSymbolData(
87 cast<MCSymbolRefExpr>(Value)->getSymbol());
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +000088 break;
89
90 case MCExpr::Unary:
91 AddValueSymbols(cast<MCUnaryExpr>(Value)->getSubExpr());
92 break;
93 }
94
95 return Value;
96 }
97
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000098 /// @name MCStreamer Interface
99 /// @{
100
101 virtual void SwitchSection(const MCSection *Section);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000102 virtual void EmitLabel(MCSymbol *Symbol);
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000103 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag);
Daniel Dunbar821e3332009-08-31 08:09:28 +0000104 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value);
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000105 virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000106 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
Chris Lattner9eb158d2010-01-23 07:47:02 +0000107 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000108 unsigned ByteAlignment);
Chris Lattnerb54b9dd2010-05-08 19:54:22 +0000109 virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol) {
110 assert(0 && "macho doesn't support this directive");
111 }
112 virtual void EmitCOFFSymbolStorageClass(int StorageClass) {
113 assert(0 && "macho doesn't support this directive");
114 }
115 virtual void EmitCOFFSymbolType(int Type) {
116 assert(0 && "macho doesn't support this directive");
117 }
118 virtual void EndCOFFSymbolDef() {
119 assert(0 && "macho doesn't support this directive");
120 }
Chris Lattner99328ad2010-01-25 07:52:13 +0000121 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
122 assert(0 && "macho doesn't support this directive");
123 }
Chris Lattner9eb158d2010-01-23 07:47:02 +0000124 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size) {
125 assert(0 && "macho doesn't support this directive");
126 }
Daniel Dunbar8751b942009-08-28 05:48:22 +0000127 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000128 unsigned Size = 0, unsigned ByteAlignment = 0);
Eric Christopher482eba02010-05-14 01:50:28 +0000129 virtual void EmitTBSSSymbol(MCSymbol *Symbol, uint64_t Size,
130 unsigned ByteAlignment = 0);
Chris Lattneraaec2052010-01-19 19:46:13 +0000131 virtual void EmitBytes(StringRef Data, unsigned AddrSpace);
Chris Lattneraaec2052010-01-19 19:46:13 +0000132 virtual void EmitValue(const MCExpr *Value, unsigned Size,unsigned AddrSpace);
Chris Lattner718fb592010-01-25 21:28:50 +0000133 virtual void EmitGPRel32Value(const MCExpr *Value) {
134 assert(0 && "macho doesn't support this directive");
135 }
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000136 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
137 unsigned ValueSize = 1,
138 unsigned MaxBytesToEmit = 0);
Kevin Enderby6e720482010-02-23 18:26:34 +0000139 virtual void EmitCodeAlignment(unsigned ByteAlignment,
140 unsigned MaxBytesToEmit = 0);
Daniel Dunbar821e3332009-08-31 08:09:28 +0000141 virtual void EmitValueToOffset(const MCExpr *Offset,
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000142 unsigned char Value = 0);
Chris Lattnera6594fc2010-01-25 18:58:59 +0000143
144 virtual void EmitFileDirective(StringRef Filename) {
145 errs() << "FIXME: MCMachoStreamer:EmitFileDirective not implemented\n";
146 }
147 virtual void EmitDwarfFileDirective(unsigned FileNo, StringRef Filename) {
148 errs() << "FIXME: MCMachoStreamer:EmitDwarfFileDirective not implemented\n";
149 }
150
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000151 virtual void EmitInstruction(const MCInst &Inst);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000152 virtual void Finish();
153
154 /// @}
155};
156
157} // end anonymous namespace.
158
159void MCMachOStreamer::SwitchSection(const MCSection *Section) {
160 assert(Section && "Cannot switch to a null section!");
Chris Lattnercda7f782009-08-22 19:35:08 +0000161
162 // If already in this section, then this is a noop.
163 if (Section == CurSection) return;
Daniel Dunbar0f5fa692009-08-28 05:48:54 +0000164
Chris Lattnercda7f782009-08-22 19:35:08 +0000165 CurSection = Section;
Daniel Dunbar46836a72010-03-10 20:58:29 +0000166 CurSectionData = &Assembler.getOrCreateSectionData(*Section);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000167}
168
169void MCMachOStreamer::EmitLabel(MCSymbol *Symbol) {
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000170 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
Daniel Dunbarc3047182010-05-05 19:01:00 +0000171 assert(!Symbol->isVariable() && "Cannot emit a variable symbol!");
172 assert(CurSection && "Cannot emit before setting section!");
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000173
Daniel Dunbar071f73d2010-05-10 22:45:09 +0000174 MCSymbolData &SD = Assembler.getOrCreateSymbolData(*Symbol);
175
176 // Update the current atom map, if necessary.
177 bool MustCreateFragment = false;
178 if (Assembler.isSymbolLinkerVisible(&SD)) {
179 CurrentAtomMap[CurSectionData] = &SD;
180
181 // We have to create a new fragment, fragments cannot span atoms.
182 MustCreateFragment = true;
183 }
184
Daniel Dunbarf70f4772010-03-22 20:35:46 +0000185 // FIXME: This is wasteful, we don't necessarily need to create a data
186 // fragment. Instead, we should mark the symbol as pointing into the data
187 // fragment if it exists, otherwise we should just queue the label and set its
188 // fragment pointer when we emit the next fragment.
Daniel Dunbar071f73d2010-05-10 22:45:09 +0000189 MCDataFragment *F =
190 MustCreateFragment ? createDataFragment() : getOrCreateDataFragment();
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000191 assert(!SD.getFragment() && "Unexpected fragment on symbol data!");
192 SD.setFragment(F);
193 SD.setOffset(F->getContents().size());
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000194
195 // This causes the reference type and weak reference flags to be cleared.
196 SD.setFlags(SD.getFlags() & ~(SF_WeakReference | SF_ReferenceTypeMask));
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000197
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000198 Symbol->setSection(*CurSection);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000199}
200
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000201void MCMachOStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
Daniel Dunbar6009db42009-08-26 21:22:22 +0000202 switch (Flag) {
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000203 case MCAF_SubsectionsViaSymbols:
Daniel Dunbar6009db42009-08-26 21:22:22 +0000204 Assembler.setSubsectionsViaSymbols(true);
Daniel Dunbar8c3eaf42009-08-28 07:08:47 +0000205 return;
Daniel Dunbar6009db42009-08-26 21:22:22 +0000206 }
Daniel Dunbar8c3eaf42009-08-28 07:08:47 +0000207
208 assert(0 && "invalid assembler flag!");
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000209}
210
Daniel Dunbar821e3332009-08-31 08:09:28 +0000211void MCMachOStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
Daniel Dunbara4d86672009-10-16 01:58:23 +0000212 // FIXME: Lift context changes into super class.
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000213 Symbol->setVariableValue(AddValueSymbols(Value));
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000214}
215
216void MCMachOStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000217 MCSymbolAttr Attribute) {
Daniel Dunbar0c7761b2009-08-24 11:56:58 +0000218 // Indirect symbols are handled differently, to match how 'as' handles
219 // them. This makes writing matching .o files easier.
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000220 if (Attribute == MCSA_IndirectSymbol) {
Daniel Dunbarad7c3d52009-08-26 00:18:21 +0000221 // Note that we intentionally cannot use the symbol data here; this is
222 // important for matching the string table that 'as' generates.
Daniel Dunbar0c7761b2009-08-24 11:56:58 +0000223 IndirectSymbolData ISD;
224 ISD.Symbol = Symbol;
225 ISD.SectionData = CurSectionData;
226 Assembler.getIndirectSymbols().push_back(ISD);
227 return;
228 }
229
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000230 // Adding a symbol attribute always introduces the symbol, note that an
Daniel Dunbar46836a72010-03-10 20:58:29 +0000231 // important side effect of calling getOrCreateSymbolData here is to register
232 // the symbol with the assembler.
233 MCSymbolData &SD = Assembler.getOrCreateSymbolData(*Symbol);
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000234
235 // The implementation of symbol attributes is designed to match 'as', but it
236 // leaves much to desired. It doesn't really make sense to arbitrarily add and
237 // remove flags, but 'as' allows this (in particular, see .desc).
238 //
239 // In the future it might be worth trying to make these operations more well
240 // defined.
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000241 switch (Attribute) {
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000242 case MCSA_Invalid:
Chris Lattnered0ab152010-01-25 18:30:45 +0000243 case MCSA_ELF_TypeFunction:
244 case MCSA_ELF_TypeIndFunction:
245 case MCSA_ELF_TypeObject:
246 case MCSA_ELF_TypeTLS:
247 case MCSA_ELF_TypeCommon:
248 case MCSA_ELF_TypeNoType:
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000249 case MCSA_IndirectSymbol:
250 case MCSA_Hidden:
251 case MCSA_Internal:
252 case MCSA_Protected:
253 case MCSA_Weak:
254 case MCSA_Local:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000255 assert(0 && "Invalid symbol attribute for Mach-O!");
256 break;
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000257
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000258 case MCSA_Global:
Daniel Dunbar8f4d1462009-08-28 07:08:35 +0000259 SD.setExternal(true);
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000260 break;
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000261
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000262 case MCSA_LazyReference:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000263 // FIXME: This requires -dynamic.
264 SD.setFlags(SD.getFlags() | SF_NoDeadStrip);
265 if (Symbol->isUndefined())
266 SD.setFlags(SD.getFlags() | SF_ReferenceTypeUndefinedLazy);
267 break;
268
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000269 // Since .reference sets the no dead strip bit, it is equivalent to
270 // .no_dead_strip in practice.
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000271 case MCSA_Reference:
272 case MCSA_NoDeadStrip:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000273 SD.setFlags(SD.getFlags() | SF_NoDeadStrip);
274 break;
275
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000276 case MCSA_PrivateExtern:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000277 SD.setExternal(true);
278 SD.setPrivateExtern(true);
279 break;
280
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000281 case MCSA_WeakReference:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000282 // FIXME: This requires -dynamic.
283 if (Symbol->isUndefined())
284 SD.setFlags(SD.getFlags() | SF_WeakReference);
285 break;
286
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000287 case MCSA_WeakDefinition:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000288 // FIXME: 'as' enforces that this is defined and global. The manual claims
289 // it has to be in a coalesced section, but this isn't enforced.
290 SD.setFlags(SD.getFlags() | SF_WeakDefinition);
291 break;
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000292 }
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000293}
294
295void MCMachOStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000296 // Encode the 'desc' value into the lowest implementation defined bits.
297 assert(DescValue == (DescValue & SF_DescFlagsMask) &&
298 "Invalid .desc value!");
Daniel Dunbar46836a72010-03-10 20:58:29 +0000299 Assembler.getOrCreateSymbolData(*Symbol).setFlags(DescValue&SF_DescFlagsMask);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000300}
301
Chris Lattner9eb158d2010-01-23 07:47:02 +0000302void MCMachOStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000303 unsigned ByteAlignment) {
Daniel Dunbar8f4d1462009-08-28 07:08:35 +0000304 // FIXME: Darwin 'as' does appear to allow redef of a .comm by itself.
305 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
306
Daniel Dunbar46836a72010-03-10 20:58:29 +0000307 MCSymbolData &SD = Assembler.getOrCreateSymbolData(*Symbol);
Daniel Dunbar8f4d1462009-08-28 07:08:35 +0000308 SD.setExternal(true);
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000309 SD.setCommon(Size, ByteAlignment);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000310}
311
Daniel Dunbar8751b942009-08-28 05:48:22 +0000312void MCMachOStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000313 unsigned Size, unsigned ByteAlignment) {
Daniel Dunbar46836a72010-03-10 20:58:29 +0000314 MCSectionData &SectData = Assembler.getOrCreateSectionData(*Section);
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000315
316 // The symbol may not be present, which only creates the section.
317 if (!Symbol)
318 return;
319
320 // FIXME: Assert that this section has the zerofill type.
321
322 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
323
Daniel Dunbar46836a72010-03-10 20:58:29 +0000324 MCSymbolData &SD = Assembler.getOrCreateSymbolData(*Symbol);
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000325
Daniel Dunbare73d49e2010-05-12 22:51:27 +0000326 // Emit an align fragment if necessary.
327 if (ByteAlignment != 1)
Daniel Dunbar1c154132010-05-12 22:56:23 +0000328 new MCAlignFragment(ByteAlignment, 0, 0, ByteAlignment, &SectData);
Daniel Dunbare73d49e2010-05-12 22:51:27 +0000329
Daniel Dunbar4e544872010-05-12 22:51:38 +0000330 MCFragment *F = new MCFillFragment(0, 0, Size, &SectData);
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000331 SD.setFragment(F);
Daniel Dunbar071f73d2010-05-10 22:45:09 +0000332 if (Assembler.isSymbolLinkerVisible(&SD))
333 F->setAtom(&SD);
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000334
335 Symbol->setSection(*Section);
336
337 // Update the maximum alignment on the zero fill section if necessary.
338 if (ByteAlignment > SectData.getAlignment())
339 SectData.setAlignment(ByteAlignment);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000340}
341
Eric Christopher482eba02010-05-14 01:50:28 +0000342void MCMachOStreamer::EmitTBSSSymbol(MCSymbol *Symbol, uint64_t Size,
343 unsigned ByteAlignment) {
344 assert(false && "Implement me!");
345}
346
Chris Lattneraaec2052010-01-19 19:46:13 +0000347void MCMachOStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {
Daniel Dunbarf70f4772010-03-22 20:35:46 +0000348 getOrCreateDataFragment()->getContents().append(Data.begin(), Data.end());
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000349}
350
Chris Lattneraaec2052010-01-19 19:46:13 +0000351void MCMachOStreamer::EmitValue(const MCExpr *Value, unsigned Size,
352 unsigned AddrSpace) {
Daniel Dunbarf70f4772010-03-22 20:35:46 +0000353 MCDataFragment *DF = getOrCreateDataFragment();
Daniel Dunbar45f48742010-02-13 09:28:22 +0000354
355 // Avoid fixups when possible.
356 int64_t AbsValue;
Daniel Dunbar40ebe2472010-02-22 22:08:57 +0000357 if (AddValueSymbols(Value)->EvaluateAsAbsolute(AbsValue)) {
Daniel Dunbar45f48742010-02-13 09:28:22 +0000358 // FIXME: Endianness assumption.
359 for (unsigned i = 0; i != Size; ++i)
360 DF->getContents().push_back(uint8_t(AbsValue >> (i * 8)));
361 } else {
Daniel Dunbar8315a352010-03-12 21:00:38 +0000362 DF->addFixup(MCAsmFixup(DF->getContents().size(), *AddValueSymbols(Value),
363 MCFixup::getKindForSize(Size)));
Daniel Dunbar45f48742010-02-13 09:28:22 +0000364 DF->getContents().resize(DF->getContents().size() + Size, 0);
365 }
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000366}
367
368void MCMachOStreamer::EmitValueToAlignment(unsigned ByteAlignment,
369 int64_t Value, unsigned ValueSize,
370 unsigned MaxBytesToEmit) {
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000371 if (MaxBytesToEmit == 0)
372 MaxBytesToEmit = ByteAlignment;
Daniel Dunbar071f73d2010-05-10 22:45:09 +0000373 MCFragment *F = new MCAlignFragment(ByteAlignment, Value, ValueSize,
Daniel Dunbar1c154132010-05-12 22:56:23 +0000374 MaxBytesToEmit, CurSectionData);
Daniel Dunbar071f73d2010-05-10 22:45:09 +0000375 F->setAtom(CurrentAtomMap.lookup(CurSectionData));
Kevin Enderby6e720482010-02-23 18:26:34 +0000376
377 // Update the maximum alignment on the current section if necessary.
378 if (ByteAlignment > CurSectionData->getAlignment())
379 CurSectionData->setAlignment(ByteAlignment);
380}
381
382void MCMachOStreamer::EmitCodeAlignment(unsigned ByteAlignment,
383 unsigned MaxBytesToEmit) {
384 if (MaxBytesToEmit == 0)
385 MaxBytesToEmit = ByteAlignment;
Daniel Dunbar1c154132010-05-12 22:56:23 +0000386 MCAlignFragment *F = new MCAlignFragment(ByteAlignment, 0, 1, MaxBytesToEmit,
387 CurSectionData);
388 F->setEmitNops(true);
Daniel Dunbar071f73d2010-05-10 22:45:09 +0000389 F->setAtom(CurrentAtomMap.lookup(CurSectionData));
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000390
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000391 // Update the maximum alignment on the current section if necessary.
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000392 if (ByteAlignment > CurSectionData->getAlignment())
393 CurSectionData->setAlignment(ByteAlignment);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000394}
395
Daniel Dunbar821e3332009-08-31 08:09:28 +0000396void MCMachOStreamer::EmitValueToOffset(const MCExpr *Offset,
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000397 unsigned char Value) {
Daniel Dunbar071f73d2010-05-10 22:45:09 +0000398 MCFragment *F = new MCOrgFragment(*Offset, Value, CurSectionData);
399 F->setAtom(CurrentAtomMap.lookup(CurSectionData));
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000400}
401
402void MCMachOStreamer::EmitInstruction(const MCInst &Inst) {
Daniel Dunbar4fac7492009-08-27 08:17:51 +0000403 // Scan for values.
404 for (unsigned i = 0; i != Inst.getNumOperands(); ++i)
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000405 if (Inst.getOperand(i).isExpr())
406 AddValueSymbols(Inst.getOperand(i).getExpr());
Daniel Dunbar4fac7492009-08-27 08:17:51 +0000407
Daniel Dunbare1ec6172010-02-02 21:44:01 +0000408 CurSectionData->setHasInstructions(true);
409
Daniel Dunbar3f4dcd92010-03-22 23:16:48 +0000410 // FIXME-PERF: Common case is that we don't need to relax, encode directly
411 // onto the data fragments buffers.
412
Daniel Dunbar73c55742010-02-09 22:59:55 +0000413 SmallVector<MCFixup, 4> Fixups;
Daniel Dunbar4fac7492009-08-27 08:17:51 +0000414 SmallString<256> Code;
415 raw_svector_ostream VecOS(Code);
Daniel Dunbarcf871e52010-03-19 10:43:18 +0000416 Assembler.getEmitter().EncodeInstruction(Inst, VecOS, Fixups);
Daniel Dunbarf6346762010-02-13 09:29:02 +0000417 VecOS.flush();
418
Daniel Dunbar337055e2010-03-23 03:13:05 +0000419 // FIXME: Eliminate this copy.
420 SmallVector<MCAsmFixup, 4> AsmFixups;
Daniel Dunbarf6346762010-02-13 09:29:02 +0000421 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
422 MCFixup &F = Fixups[i];
Daniel Dunbar337055e2010-03-23 03:13:05 +0000423 AsmFixups.push_back(MCAsmFixup(F.getOffset(), *F.getValue(),
424 F.getKind()));
425 }
426
Daniel Dunbard8036fb2010-03-23 05:09:03 +0000427 // See if we might need to relax this instruction, if so it needs its own
428 // fragment.
429 //
430 // FIXME-PERF: Support target hook to do a fast path that avoids the encoder,
431 // when we can immediately tell that we will get something which might need
432 // relaxation (and compute its size).
433 //
434 // FIXME-PERF: We should also be smart about immediately relaxing instructions
435 // which we can already show will never possibly fit (we can also do a very
436 // good job of this before we do the first relaxation pass, because we have
437 // total knowledge about undefined symbols at that point). Even now, though,
438 // we can do a decent job, especially on Darwin where scattering means that we
439 // are going to often know that we can never fully resolve a fixup.
440 if (Assembler.getBackend().MayNeedRelaxation(Inst, AsmFixups)) {
441 MCInstFragment *IF = new MCInstFragment(Inst, CurSectionData);
Daniel Dunbar071f73d2010-05-10 22:45:09 +0000442 IF->setAtom(CurrentAtomMap.lookup(CurSectionData));
Daniel Dunbard8036fb2010-03-23 05:09:03 +0000443
444 // Add the fixups and data.
445 //
446 // FIXME: Revisit this design decision when relaxation is done, we may be
447 // able to get away with not storing any extra data in the MCInst.
448 IF->getCode() = Code;
449 IF->getFixups() = AsmFixups;
450
451 return;
452 }
453
Daniel Dunbar337055e2010-03-23 03:13:05 +0000454 // Add the fixups and data.
455 MCDataFragment *DF = getOrCreateDataFragment();
456 for (unsigned i = 0, e = AsmFixups.size(); i != e; ++i) {
457 AsmFixups[i].Offset += DF->getContents().size();
458 DF->addFixup(AsmFixups[i]);
Daniel Dunbarf6346762010-02-13 09:29:02 +0000459 }
460 DF->getContents().append(Code.begin(), Code.end());
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000461}
462
463void MCMachOStreamer::Finish() {
464 Assembler.Finish();
465}
466
Daniel Dunbar1f3e4452010-03-11 01:34:27 +0000467MCStreamer *llvm::createMachOStreamer(MCContext &Context, TargetAsmBackend &TAB,
Daniel Dunbarac2884a2010-03-25 22:49:09 +0000468 raw_ostream &OS, MCCodeEmitter *CE,
469 bool RelaxAll) {
470 MCMachOStreamer *S = new MCMachOStreamer(Context, TAB, OS, CE);
471 if (RelaxAll)
472 S->getAssembler().setRelaxAll(true);
473 return S;
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000474}