Silence a GCC warning about not handling all flags in this switch, we
[llvm/stm8.git] / lib / MC / MCMachOStreamer.cpp
bloba0695058e228b452dc2ab4ec15f1b87a2d7950d1
1 //===- 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 //===----------------------------------------------------------------------===//
10 #include "llvm/MC/MCStreamer.h"
12 #include "llvm/MC/MCAssembler.h"
13 #include "llvm/MC/MCContext.h"
14 #include "llvm/MC/MCCodeEmitter.h"
15 #include "llvm/MC/MCExpr.h"
16 #include "llvm/MC/MCInst.h"
17 #include "llvm/MC/MCObjectStreamer.h"
18 #include "llvm/MC/MCSection.h"
19 #include "llvm/MC/MCSymbol.h"
20 #include "llvm/MC/MCMachOSymbolFlags.h"
21 #include "llvm/MC/MCSectionMachO.h"
22 #include "llvm/MC/MCDwarf.h"
23 #include "llvm/Support/ErrorHandling.h"
24 #include "llvm/Support/raw_ostream.h"
25 #include "llvm/Target/TargetAsmBackend.h"
27 using namespace llvm;
29 namespace {
31 class MCMachOStreamer : public MCObjectStreamer {
32 private:
33 void EmitInstToFragment(const MCInst &Inst);
34 void EmitInstToData(const MCInst &Inst);
35 // FIXME: These will likely moved to a better place.
36 void MakeLineEntryForSection(const MCSection *Section);
37 const MCExpr * MakeStartMinusEndExpr(MCSymbol *Start, MCSymbol *End,
38 int IntVal);
39 void EmitDwarfFileTable(void);
41 public:
42 MCMachOStreamer(MCContext &Context, TargetAsmBackend &TAB,
43 raw_ostream &OS, MCCodeEmitter *Emitter)
44 : MCObjectStreamer(Context, TAB, OS, Emitter, true) {}
46 /// @name MCStreamer Interface
47 /// @{
49 virtual void InitSections();
50 virtual void EmitLabel(MCSymbol *Symbol);
51 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag);
52 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value);
53 virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute);
54 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
55 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
56 unsigned ByteAlignment);
57 virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol) {
58 assert(0 && "macho doesn't support this directive");
60 virtual void EmitCOFFSymbolStorageClass(int StorageClass) {
61 assert(0 && "macho doesn't support this directive");
63 virtual void EmitCOFFSymbolType(int Type) {
64 assert(0 && "macho doesn't support this directive");
66 virtual void EndCOFFSymbolDef() {
67 assert(0 && "macho doesn't support this directive");
69 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
70 assert(0 && "macho doesn't support this directive");
72 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size) {
73 assert(0 && "macho doesn't support this directive");
75 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
76 unsigned Size = 0, unsigned ByteAlignment = 0);
77 virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
78 uint64_t Size, unsigned ByteAlignment = 0);
79 virtual void EmitBytes(StringRef Data, unsigned AddrSpace);
80 virtual void EmitValue(const MCExpr *Value, unsigned Size,unsigned AddrSpace);
81 virtual void EmitGPRel32Value(const MCExpr *Value) {
82 assert(0 && "macho doesn't support this directive");
84 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
85 unsigned ValueSize = 1,
86 unsigned MaxBytesToEmit = 0);
87 virtual void EmitCodeAlignment(unsigned ByteAlignment,
88 unsigned MaxBytesToEmit = 0);
89 virtual void EmitValueToOffset(const MCExpr *Offset,
90 unsigned char Value = 0);
92 virtual void EmitFileDirective(StringRef Filename) {
93 // FIXME: Just ignore the .file; it isn't important enough to fail the
94 // entire assembly.
96 //report_fatal_error("unsupported directive: '.file'");
98 virtual void EmitDwarfFileDirective(unsigned FileNo, StringRef Filename) {
99 // FIXME: Just ignore the .file; it isn't important enough to fail the
100 // entire assembly.
102 //report_fatal_error("unsupported directive: '.file'");
105 virtual void EmitInstruction(const MCInst &Inst);
107 virtual void Finish();
109 /// @}
112 } // end anonymous namespace.
114 void MCMachOStreamer::InitSections() {
115 SwitchSection(getContext().getMachOSection("__TEXT", "__text",
116 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
117 0, SectionKind::getText()));
121 void MCMachOStreamer::EmitLabel(MCSymbol *Symbol) {
122 // TODO: This is almost exactly the same as WinCOFFStreamer. Consider merging
123 // into MCObjectStreamer.
124 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
125 assert(!Symbol->isVariable() && "Cannot emit a variable symbol!");
126 assert(CurSection && "Cannot emit before setting section!");
128 Symbol->setSection(*CurSection);
130 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
132 // We have to create a new fragment if this is an atom defining symbol,
133 // fragments cannot span atoms.
134 if (getAssembler().isSymbolLinkerVisible(SD.getSymbol()))
135 new MCDataFragment(getCurrentSectionData());
137 // FIXME: This is wasteful, we don't necessarily need to create a data
138 // fragment. Instead, we should mark the symbol as pointing into the data
139 // fragment if it exists, otherwise we should just queue the label and set its
140 // fragment pointer when we emit the next fragment.
141 MCDataFragment *F = getOrCreateDataFragment();
142 assert(!SD.getFragment() && "Unexpected fragment on symbol data!");
143 SD.setFragment(F);
144 SD.setOffset(F->getContents().size());
146 // This causes the reference type flag to be cleared. Darwin 'as' was "trying"
147 // to clear the weak reference and weak definition bits too, but the
148 // implementation was buggy. For now we just try to match 'as', for
149 // diffability.
151 // FIXME: Cleanup this code, these bits should be emitted based on semantic
152 // properties, not on the order of definition, etc.
153 SD.setFlags(SD.getFlags() & ~SF_ReferenceTypeMask);
156 void MCMachOStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
157 switch (Flag) {
158 case MCAF_SubsectionsViaSymbols:
159 getAssembler().setSubsectionsViaSymbols(true);
160 return;
161 default:
162 llvm_unreachable("invalid assembler flag!");
166 void MCMachOStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
167 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
168 // MCObjectStreamer.
169 // FIXME: Lift context changes into super class.
170 getAssembler().getOrCreateSymbolData(*Symbol);
171 Symbol->setVariableValue(AddValueSymbols(Value));
174 void MCMachOStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
175 MCSymbolAttr Attribute) {
176 // Indirect symbols are handled differently, to match how 'as' handles
177 // them. This makes writing matching .o files easier.
178 if (Attribute == MCSA_IndirectSymbol) {
179 // Note that we intentionally cannot use the symbol data here; this is
180 // important for matching the string table that 'as' generates.
181 IndirectSymbolData ISD;
182 ISD.Symbol = Symbol;
183 ISD.SectionData = getCurrentSectionData();
184 getAssembler().getIndirectSymbols().push_back(ISD);
185 return;
188 // Adding a symbol attribute always introduces the symbol, note that an
189 // important side effect of calling getOrCreateSymbolData here is to register
190 // the symbol with the assembler.
191 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
193 // The implementation of symbol attributes is designed to match 'as', but it
194 // leaves much to desired. It doesn't really make sense to arbitrarily add and
195 // remove flags, but 'as' allows this (in particular, see .desc).
197 // In the future it might be worth trying to make these operations more well
198 // defined.
199 switch (Attribute) {
200 case MCSA_Invalid:
201 case MCSA_ELF_TypeFunction:
202 case MCSA_ELF_TypeIndFunction:
203 case MCSA_ELF_TypeObject:
204 case MCSA_ELF_TypeTLS:
205 case MCSA_ELF_TypeCommon:
206 case MCSA_ELF_TypeNoType:
207 case MCSA_IndirectSymbol:
208 case MCSA_Hidden:
209 case MCSA_Internal:
210 case MCSA_Protected:
211 case MCSA_Weak:
212 case MCSA_Local:
213 assert(0 && "Invalid symbol attribute for Mach-O!");
214 break;
216 case MCSA_Global:
217 SD.setExternal(true);
218 // This effectively clears the undefined lazy bit, in Darwin 'as', although
219 // it isn't very consistent because it implements this as part of symbol
220 // lookup.
222 // FIXME: Cleanup this code, these bits should be emitted based on semantic
223 // properties, not on the order of definition, etc.
224 SD.setFlags(SD.getFlags() & ~SF_ReferenceTypeUndefinedLazy);
225 break;
227 case MCSA_LazyReference:
228 // FIXME: This requires -dynamic.
229 SD.setFlags(SD.getFlags() | SF_NoDeadStrip);
230 if (Symbol->isUndefined())
231 SD.setFlags(SD.getFlags() | SF_ReferenceTypeUndefinedLazy);
232 break;
234 // Since .reference sets the no dead strip bit, it is equivalent to
235 // .no_dead_strip in practice.
236 case MCSA_Reference:
237 case MCSA_NoDeadStrip:
238 SD.setFlags(SD.getFlags() | SF_NoDeadStrip);
239 break;
241 case MCSA_PrivateExtern:
242 SD.setExternal(true);
243 SD.setPrivateExtern(true);
244 break;
246 case MCSA_WeakReference:
247 // FIXME: This requires -dynamic.
248 if (Symbol->isUndefined())
249 SD.setFlags(SD.getFlags() | SF_WeakReference);
250 break;
252 case MCSA_WeakDefinition:
253 // FIXME: 'as' enforces that this is defined and global. The manual claims
254 // it has to be in a coalesced section, but this isn't enforced.
255 SD.setFlags(SD.getFlags() | SF_WeakDefinition);
256 break;
258 case MCSA_WeakDefAutoPrivate:
259 SD.setFlags(SD.getFlags() | SF_WeakDefinition | SF_WeakReference);
260 break;
264 void MCMachOStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
265 // Encode the 'desc' value into the lowest implementation defined bits.
266 assert(DescValue == (DescValue & SF_DescFlagsMask) &&
267 "Invalid .desc value!");
268 getAssembler().getOrCreateSymbolData(*Symbol).setFlags(
269 DescValue & SF_DescFlagsMask);
272 void MCMachOStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
273 unsigned ByteAlignment) {
274 // FIXME: Darwin 'as' does appear to allow redef of a .comm by itself.
275 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
277 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
278 SD.setExternal(true);
279 SD.setCommon(Size, ByteAlignment);
282 void MCMachOStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
283 unsigned Size, unsigned ByteAlignment) {
284 MCSectionData &SectData = getAssembler().getOrCreateSectionData(*Section);
286 // The symbol may not be present, which only creates the section.
287 if (!Symbol)
288 return;
290 // FIXME: Assert that this section has the zerofill type.
292 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
294 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
296 // Emit an align fragment if necessary.
297 if (ByteAlignment != 1)
298 new MCAlignFragment(ByteAlignment, 0, 0, ByteAlignment, &SectData);
300 MCFragment *F = new MCFillFragment(0, 0, Size, &SectData);
301 SD.setFragment(F);
303 Symbol->setSection(*Section);
305 // Update the maximum alignment on the zero fill section if necessary.
306 if (ByteAlignment > SectData.getAlignment())
307 SectData.setAlignment(ByteAlignment);
310 // This should always be called with the thread local bss section. Like the
311 // .zerofill directive this doesn't actually switch sections on us.
312 void MCMachOStreamer::EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
313 uint64_t Size, unsigned ByteAlignment) {
314 EmitZerofill(Section, Symbol, Size, ByteAlignment);
315 return;
318 void MCMachOStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {
319 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
320 // MCObjectStreamer.
321 getOrCreateDataFragment()->getContents().append(Data.begin(), Data.end());
324 void MCMachOStreamer::EmitValue(const MCExpr *Value, unsigned Size,
325 unsigned AddrSpace) {
326 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
327 // MCObjectStreamer.
328 MCDataFragment *DF = getOrCreateDataFragment();
330 // Avoid fixups when possible.
331 int64_t AbsValue;
332 if (AddValueSymbols(Value)->EvaluateAsAbsolute(AbsValue)) {
333 // FIXME: Endianness assumption.
334 for (unsigned i = 0; i != Size; ++i)
335 DF->getContents().push_back(uint8_t(AbsValue >> (i * 8)));
336 } else {
337 DF->addFixup(MCFixup::Create(DF->getContents().size(),
338 AddValueSymbols(Value),
339 MCFixup::getKindForSize(Size)));
340 DF->getContents().resize(DF->getContents().size() + Size, 0);
344 void MCMachOStreamer::EmitValueToAlignment(unsigned ByteAlignment,
345 int64_t Value, unsigned ValueSize,
346 unsigned MaxBytesToEmit) {
347 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
348 // MCObjectStreamer.
349 if (MaxBytesToEmit == 0)
350 MaxBytesToEmit = ByteAlignment;
351 new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit,
352 getCurrentSectionData());
354 // Update the maximum alignment on the current section if necessary.
355 if (ByteAlignment > getCurrentSectionData()->getAlignment())
356 getCurrentSectionData()->setAlignment(ByteAlignment);
359 void MCMachOStreamer::EmitCodeAlignment(unsigned ByteAlignment,
360 unsigned MaxBytesToEmit) {
361 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
362 // MCObjectStreamer.
363 if (MaxBytesToEmit == 0)
364 MaxBytesToEmit = ByteAlignment;
365 MCAlignFragment *F = new MCAlignFragment(ByteAlignment, 0, 1, MaxBytesToEmit,
366 getCurrentSectionData());
367 F->setEmitNops(true);
369 // Update the maximum alignment on the current section if necessary.
370 if (ByteAlignment > getCurrentSectionData()->getAlignment())
371 getCurrentSectionData()->setAlignment(ByteAlignment);
374 void MCMachOStreamer::EmitValueToOffset(const MCExpr *Offset,
375 unsigned char Value) {
376 new MCOrgFragment(*Offset, Value, getCurrentSectionData());
379 void MCMachOStreamer::EmitInstToFragment(const MCInst &Inst) {
380 MCInstFragment *IF = new MCInstFragment(Inst, getCurrentSectionData());
382 // Add the fixups and data.
384 // FIXME: Revisit this design decision when relaxation is done, we may be
385 // able to get away with not storing any extra data in the MCInst.
386 SmallVector<MCFixup, 4> Fixups;
387 SmallString<256> Code;
388 raw_svector_ostream VecOS(Code);
389 getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups);
390 VecOS.flush();
392 IF->getCode() = Code;
393 IF->getFixups() = Fixups;
396 void MCMachOStreamer::EmitInstToData(const MCInst &Inst) {
397 MCDataFragment *DF = getOrCreateDataFragment();
399 SmallVector<MCFixup, 4> Fixups;
400 SmallString<256> Code;
401 raw_svector_ostream VecOS(Code);
402 getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups);
403 VecOS.flush();
405 // Add the fixups and data.
406 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
407 Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size());
408 DF->addFixup(Fixups[i]);
410 DF->getContents().append(Code.begin(), Code.end());
413 void MCMachOStreamer::EmitInstruction(const MCInst &Inst) {
414 // Scan for values.
415 for (unsigned i = Inst.getNumOperands(); i--; )
416 if (Inst.getOperand(i).isExpr())
417 AddValueSymbols(Inst.getOperand(i).getExpr());
419 getCurrentSectionData()->setHasInstructions(true);
421 // Now that a machine instruction has been assembled into this section, make
422 // a line entry for any .loc directive that has been seen.
423 MakeLineEntryForSection(getCurrentSection());
425 // If this instruction doesn't need relaxation, just emit it as data.
426 if (!getAssembler().getBackend().MayNeedRelaxation(Inst)) {
427 EmitInstToData(Inst);
428 return;
431 // Otherwise, if we are relaxing everything, relax the instruction as much as
432 // possible and emit it as data.
433 if (getAssembler().getRelaxAll()) {
434 MCInst Relaxed;
435 getAssembler().getBackend().RelaxInstruction(Inst, Relaxed);
436 while (getAssembler().getBackend().MayNeedRelaxation(Relaxed))
437 getAssembler().getBackend().RelaxInstruction(Relaxed, Relaxed);
438 EmitInstToData(Relaxed);
439 return;
442 // Otherwise emit to a separate fragment.
443 EmitInstToFragment(Inst);
447 // This is called when an instruction is assembled into the specified section
448 // and if there is information from the last .loc directive that has yet to have
449 // a line entry made for it is made.
451 void MCMachOStreamer::MakeLineEntryForSection(const MCSection *Section) {
452 if (!getContext().getDwarfLocSeen())
453 return;
455 // Create a symbol at in the current section for use in the line entry.
456 MCSymbol *LineSym = getContext().CreateTempSymbol();
457 // Set the value of the symbol to use for the MCLineEntry.
458 EmitLabel(LineSym);
460 // Get the current .loc info saved in the context.
461 const MCDwarfLoc &DwarfLoc = getContext().getCurrentDwarfLoc();
463 // Create a (local) line entry with the symbol and the current .loc info.
464 MCLineEntry LineEntry(LineSym, DwarfLoc);
466 // clear DwarfLocSeen saying the current .loc info is now used.
467 getContext().clearDwarfLocSeen();
469 // Get the MCLineSection for this section, if one does not exist for this
470 // section create it.
471 DenseMap<const MCSection *, MCLineSection *> &MCLineSections =
472 getContext().getMCLineSections();
473 MCLineSection *LineSection = MCLineSections[Section];
474 if (!LineSection) {
475 // Create a new MCLineSection. This will be deleted after the dwarf line
476 // table is created using it by iterating through the MCLineSections
477 // DenseMap.
478 LineSection = new MCLineSection;
479 // Save a pointer to the new LineSection into the MCLineSections DenseMap.
480 MCLineSections[Section] = LineSection;
483 // Add the line entry to this section's entries.
484 LineSection->addLineEntry(LineEntry);
488 // This helper routine returns an expression of End - Start + IntVal for use
489 // by EmitDwarfFileTable() below.
491 const MCExpr * MCMachOStreamer::MakeStartMinusEndExpr(MCSymbol *Start,
492 MCSymbol *End,
493 int IntVal) {
494 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
495 const MCExpr *Res =
496 MCSymbolRefExpr::Create(End, Variant, getContext());
497 const MCExpr *RHS =
498 MCSymbolRefExpr::Create(Start, Variant, getContext());
499 const MCExpr *Res1 =
500 MCBinaryExpr::Create(MCBinaryExpr::Sub, Res, RHS,getContext());
501 const MCExpr *Res2 =
502 MCConstantExpr::Create(IntVal, getContext());
503 const MCExpr *Res3 =
504 MCBinaryExpr::Create(MCBinaryExpr::Sub, Res1, Res2, getContext());
505 return Res3;
509 // This emits the Dwarf file (and eventually the line) table.
511 void MCMachOStreamer::EmitDwarfFileTable(void) {
512 // For now make sure we don't put out the Dwarf file table if no .file
513 // directives were seen.
514 const std::vector<MCDwarfFile *> &MCDwarfFiles =
515 getContext().getMCDwarfFiles();
516 if (MCDwarfFiles.size() == 0)
517 return;
519 // This is the Mach-O section, for ELF it is the .debug_line section.
520 SwitchSection(getContext().getMachOSection("__DWARF", "__debug_line",
521 MCSectionMachO::S_ATTR_DEBUG,
522 0, SectionKind::getDataRelLocal()));
524 // Create a symbol at the beginning of this section.
525 MCSymbol *LineStartSym = getContext().CreateTempSymbol();
526 // Set the value of the symbol, as we are at the start of the section.
527 EmitLabel(LineStartSym);
529 // Create a symbol for the end of the section (to be set when we get there).
530 MCSymbol *LineEndSym = getContext().CreateTempSymbol();
532 // The first 4 bytes is the total length of the information for this
533 // compilation unit (not including these 4 bytes for the length).
534 EmitValue(MakeStartMinusEndExpr(LineStartSym, LineEndSym, 4), 4, 0);
536 // Next 2 bytes is the Version, which is Dwarf 2.
537 EmitIntValue(2, 2);
539 // Create a symbol for the end of the prologue (to be set when we get there).
540 MCSymbol *ProEndSym = getContext().CreateTempSymbol(); // Lprologue_end
542 // Length of the prologue, is the next 4 bytes. Which is the start of the
543 // section to the end of the prologue. Not including the 4 bytes for the
544 // total length, the 2 bytes for the version, and these 4 bytes for the
545 // length of the prologue.
546 EmitValue(MakeStartMinusEndExpr(LineStartSym, ProEndSym, (4 + 2 + 4)), 4, 0);
548 // Parameters of the state machine, are next.
549 // Define the architecture-dependent minimum instruction length (in
550 // bytes). This value should be rather too small than too big. */
551 // DWARF2_LINE_MIN_INSN_LENGTH
552 EmitIntValue(1, 1);
553 // Flag that indicates the initial value of the is_stmt_start flag.
554 // DWARF2_LINE_DEFAULT_IS_STMT
555 EmitIntValue(1, 1);
556 // Minimum line offset in a special line info. opcode. This value
557 // was chosen to give a reasonable range of values. */
558 // DWARF2_LINE_BASE
559 EmitIntValue(uint64_t(-5), 1);
560 // Range of line offsets in a special line info. opcode.
561 // DWARF2_LINE_RANGE
562 EmitIntValue(14, 1);
563 // First special line opcode - leave room for the standard opcodes.
564 // DWARF2_LINE_OPCODE_BASE
565 EmitIntValue(13, 1);
567 // Standard opcode lengths
568 EmitIntValue(0, 1); // length of DW_LNS_copy
569 EmitIntValue(1, 1); // length of DW_LNS_advance_pc
570 EmitIntValue(1, 1); // length of DW_LNS_advance_line
571 EmitIntValue(1, 1); // length of DW_LNS_set_file
572 EmitIntValue(1, 1); // length of DW_LNS_set_column
573 EmitIntValue(0, 1); // length of DW_LNS_negate_stmt
574 EmitIntValue(0, 1); // length of DW_LNS_set_basic_block
575 EmitIntValue(0, 1); // length of DW_LNS_const_add_pc
576 EmitIntValue(1, 1); // length of DW_LNS_fixed_advance_pc
577 EmitIntValue(0, 1); // length of DW_LNS_set_prologue_end
578 EmitIntValue(0, 1); // length of DW_LNS_set_epilogue_begin
579 EmitIntValue(1, 1); // DW_LNS_set_isa
581 // Put out the directory and file tables.
583 // First the directory table.
584 const std::vector<StringRef> &MCDwarfDirs =
585 getContext().getMCDwarfDirs();
586 for (unsigned i = 0; i < MCDwarfDirs.size(); i++) {
587 EmitBytes(MCDwarfDirs[i], 0); // the DirectoryName
588 EmitBytes(StringRef("\0", 1), 0); // the null termination of the string
590 EmitIntValue(0, 1); // Terminate the directory list
592 // Second the file table.
593 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
594 EmitBytes(MCDwarfFiles[i]->getName(), 0); // FileName
595 EmitBytes(StringRef("\0", 1), 0); // the null termination of the string
596 // FIXME the Directory number should be a .uleb128 not a .byte
597 EmitIntValue(MCDwarfFiles[i]->getDirIndex(), 1);
598 EmitIntValue(0, 1); // last modification timestamp (always 0)
599 EmitIntValue(0, 1); // filesize (always 0)
601 EmitIntValue(0, 1); // Terminate the file list
603 // This is the end of the prologue, so set the value of the symbol at the
604 // end of the prologue (that was used in a previous expression).
605 EmitLabel(ProEndSym);
607 // TODO: This is the point where the line tables would be emitted.
609 // Delete the MCLineSections that were created in
610 // MCMachOStreamer::MakeLineEntryForSection() and used to emit the line
611 // tables.
612 DenseMap<const MCSection *, MCLineSection *> &MCLineSections =
613 getContext().getMCLineSections();
614 for (DenseMap<const MCSection *, MCLineSection *>::iterator it =
615 MCLineSections.begin(), ie = MCLineSections.end(); it != ie; ++it) {
616 delete it->second;
619 // If there are no line tables emited then we emit:
620 // The following DW_LNE_set_address sequence to set the address to zero
621 // TODO test for 32-bit or 64-bit output
622 // This is the sequence for 32-bit code
623 EmitIntValue(0, 1);
624 EmitIntValue(5, 1);
625 EmitIntValue(2, 1);
626 EmitIntValue(0, 1);
627 EmitIntValue(0, 1);
628 EmitIntValue(0, 1);
629 EmitIntValue(0, 1);
631 // Lastly emit the DW_LNE_end_sequence which consists of 3 bytes '00 01 01'
632 // (00 is the code for extended opcodes, followed by a ULEB128 length of the
633 // extended opcode (01), and the DW_LNE_end_sequence (01).
634 EmitIntValue(0, 1); // DW_LNS_extended_op
635 EmitIntValue(1, 1); // ULEB128 length of the extended opcode
636 EmitIntValue(1, 1); // DW_LNE_end_sequence
638 // This is the end of the section, so set the value of the symbol at the end
639 // of this section (that was used in a previous expression).
640 EmitLabel(LineEndSym);
643 void MCMachOStreamer::Finish() {
644 // Dump out the dwarf file and directory tables (soon to include line table)
645 EmitDwarfFileTable();
647 // We have to set the fragment atom associations so we can relax properly for
648 // Mach-O.
650 // First, scan the symbol table to build a lookup table from fragments to
651 // defining symbols.
652 DenseMap<const MCFragment*, MCSymbolData*> DefiningSymbolMap;
653 for (MCAssembler::symbol_iterator it = getAssembler().symbol_begin(),
654 ie = getAssembler().symbol_end(); it != ie; ++it) {
655 if (getAssembler().isSymbolLinkerVisible(it->getSymbol()) &&
656 it->getFragment()) {
657 // An atom defining symbol should never be internal to a fragment.
658 assert(it->getOffset() == 0 && "Invalid offset in atom defining symbol!");
659 DefiningSymbolMap[it->getFragment()] = it;
663 // Set the fragment atom associations by tracking the last seen atom defining
664 // symbol.
665 for (MCAssembler::iterator it = getAssembler().begin(),
666 ie = getAssembler().end(); it != ie; ++it) {
667 MCSymbolData *CurrentAtom = 0;
668 for (MCSectionData::iterator it2 = it->begin(),
669 ie2 = it->end(); it2 != ie2; ++it2) {
670 if (MCSymbolData *SD = DefiningSymbolMap.lookup(it2))
671 CurrentAtom = SD;
672 it2->setAtom(CurrentAtom);
676 this->MCObjectStreamer::Finish();
679 MCStreamer *llvm::createMachOStreamer(MCContext &Context, TargetAsmBackend &TAB,
680 raw_ostream &OS, MCCodeEmitter *CE,
681 bool RelaxAll) {
682 MCMachOStreamer *S = new MCMachOStreamer(Context, TAB, OS, CE);
683 if (RelaxAll)
684 S->getAssembler().setRelaxAll(true);
685 return S;