MC: Eliminate MCZeroFillFragment, it is no longer needed.
[oota-llvm.git] / lib / MC / MCAssembler.cpp
index 60349e69aa21624086286aa20d55c4ff624572e0..d5ff94996fb25ed9211202eac2abfb07bfffa8bf 100644 (file)
@@ -68,12 +68,9 @@ void MCAsmLayout::UpdateForSlide(MCFragment *F, int SlideAmount) {
   //
   // FIXME-PERF: This is O(N^2), but will be eliminated once we get smarter.
 
-  // Layout the concrete sections and fragments.
-  uint64_t Address = 0;
-  for (iterator it = begin(), ie = end(); it != ie; ++it) {
-    // Layout the section fragments and its size.
-    Address = getAssembler().LayoutSection(**it, *this, Address);
-  }
+  // Layout the sections in order.
+  for (unsigned i = 0, e = getSectionOrder().size(); i != e; ++i)
+    getAssembler().LayoutSection(*this, i);
 }
 
 uint64_t MCAsmLayout::getFragmentAddress(const MCFragment *F) const {
@@ -365,112 +362,110 @@ bool MCAssembler::EvaluateFixup(const MCAsmLayout &Layout,
   return IsResolved;
 }
 
-uint64_t MCAssembler::LayoutSection(MCSectionData &SD,
-                                    MCAsmLayout &Layout,
-                                    uint64_t StartAddress) {
-  bool IsVirtual = getBackend().isVirtualSection(SD.getSection());
-
-  ++stats::SectionLayouts;
+void MCAssembler::LayoutFragment(MCAsmLayout &Layout, MCFragment &F) {
+  uint64_t StartAddress = Layout.getSectionAddress(F.getParent());
 
-  // Align this section if necessary by adding padding bytes to the previous
-  // section. It is safe to adjust this out-of-band, because no symbol or
-  // fragment is allowed to point past the end of the section at any time.
-  if (uint64_t Pad = OffsetToAlignment(StartAddress, SD.getAlignment())) {
-    // Unless this section is virtual (where we are allowed to adjust the offset
-    // freely), the padding goes in the previous section.
-    if (!IsVirtual) {
-      // Find the previous non-virtual section.
-      iterator it = &SD;
-      assert(it != begin() && "Invalid initial section address!");
-      for (--it; getBackend().isVirtualSection(it->getSection()); --it) ;
-      Layout.setSectionFileSize(&*it, Layout.getSectionFileSize(&*it) + Pad);
-    }
-
-    StartAddress += Pad;
-  }
+  // Get the fragment start address.
+  uint64_t Address = StartAddress;
+  MCSectionData::iterator it = &F;
+  if (MCFragment *Prev = F.getPrevNode())
+    Address = (StartAddress + Layout.getFragmentOffset(Prev) +
+               Layout.getFragmentEffectiveSize(Prev));
 
-  // Set the aligned section address.
-  Layout.setSectionAddress(&SD, StartAddress);
+  ++stats::FragmentLayouts;
 
-  uint64_t Address = StartAddress;
-  for (MCSectionData::iterator it = SD.begin(), ie = SD.end(); it != ie; ++it) {
-    MCFragment &F = *it;
+  uint64_t FragmentOffset = Address - StartAddress;
+  Layout.setFragmentOffset(&F, FragmentOffset);
 
-    ++stats::FragmentLayouts;
+  // Evaluate fragment size.
+  uint64_t EffectiveSize = 0;
+  switch (F.getKind()) {
+  case MCFragment::FT_Align: {
+    MCAlignFragment &AF = cast<MCAlignFragment>(F);
 
-    uint64_t FragmentOffset = Address - StartAddress;
-    Layout.setFragmentOffset(&F, FragmentOffset);
+    EffectiveSize = OffsetToAlignment(Address, AF.getAlignment());
+    if (EffectiveSize > AF.getMaxBytesToEmit())
+      EffectiveSize = 0;
+    break;
+  }
 
-    // Evaluate fragment size.
-    uint64_t EffectiveSize = 0;
-    switch (F.getKind()) {
-    case MCFragment::FT_Align: {
-      MCAlignFragment &AF = cast<MCAlignFragment>(F);
+  case MCFragment::FT_Data:
+    EffectiveSize = cast<MCDataFragment>(F).getContents().size();
+    break;
 
-      EffectiveSize = OffsetToAlignment(Address, AF.getAlignment());
-      if (EffectiveSize > AF.getMaxBytesToEmit())
-        EffectiveSize = 0;
-      break;
-    }
+  case MCFragment::FT_Fill: {
+    EffectiveSize = cast<MCFillFragment>(F).getSize();
+    break;
+  }
 
-    case MCFragment::FT_Data:
-      EffectiveSize = cast<MCDataFragment>(F).getContents().size();
-      break;
+  case MCFragment::FT_Inst:
+    EffectiveSize = cast<MCInstFragment>(F).getInstSize();
+    break;
 
-    case MCFragment::FT_Fill: {
-      MCFillFragment &FF = cast<MCFillFragment>(F);
-      EffectiveSize = FF.getValueSize() * FF.getCount();
-      break;
-    }
+  case MCFragment::FT_Org: {
+    MCOrgFragment &OF = cast<MCOrgFragment>(F);
 
-    case MCFragment::FT_Inst:
-      EffectiveSize = cast<MCInstFragment>(F).getInstSize();
-      break;
+    int64_t TargetLocation;
+    if (!OF.getOffset().EvaluateAsAbsolute(TargetLocation, &Layout))
+      report_fatal_error("expected assembly-time absolute expression");
 
-    case MCFragment::FT_Org: {
-      MCOrgFragment &OF = cast<MCOrgFragment>(F);
+    // FIXME: We need a way to communicate this error.
+    int64_t Offset = TargetLocation - FragmentOffset;
+    if (Offset < 0)
+      report_fatal_error("invalid .org offset '" + Twine(TargetLocation) +
+                         "' (at offset '" + Twine(FragmentOffset) + "'");
 
-      int64_t TargetLocation;
-      if (!OF.getOffset().EvaluateAsAbsolute(TargetLocation, &Layout))
-        report_fatal_error("expected assembly-time absolute expression");
+    EffectiveSize = Offset;
+    break;
+  }
+  }
 
-      // FIXME: We need a way to communicate this error.
-      int64_t Offset = TargetLocation - FragmentOffset;
-      if (Offset < 0)
-        report_fatal_error("invalid .org offset '" + Twine(TargetLocation) +
-                          "' (at offset '" + Twine(FragmentOffset) + "'");
+  Layout.setFragmentEffectiveSize(&F, EffectiveSize);
+}
 
-      EffectiveSize = Offset;
-      break;
-    }
+void MCAssembler::LayoutSection(MCAsmLayout &Layout,
+                                unsigned SectionOrderIndex) {
+  MCSectionData &SD = *Layout.getSectionOrder()[SectionOrderIndex];
+  bool IsVirtual = getBackend().isVirtualSection(SD.getSection());
 
-    case MCFragment::FT_ZeroFill: {
-      MCZeroFillFragment &ZFF = cast<MCZeroFillFragment>(F);
+  ++stats::SectionLayouts;
 
-      // Align the fragment offset; it is safe to adjust the offset freely since
-      // this is only in virtual sections.
-      //
-      // FIXME: We shouldn't be doing this here.
-      Address = RoundUpToAlignment(Address, ZFF.getAlignment());
-      Layout.setFragmentOffset(&F, Address - StartAddress);
+  // Get the section start address.
+  uint64_t StartAddress = 0;
+  if (SectionOrderIndex) {
+    MCSectionData *Prev = Layout.getSectionOrder()[SectionOrderIndex - 1];
+    StartAddress = Layout.getSectionAddress(Prev) + Layout.getSectionSize(Prev);
+  }
 
-      EffectiveSize = ZFF.getSize();
-      break;
-    }
+  // Align this section if necessary by adding padding bytes to the previous
+  // section. It is safe to adjust this out-of-band, because no symbol or
+  // fragment is allowed to point past the end of the section at any time.
+  if (uint64_t Pad = OffsetToAlignment(StartAddress, SD.getAlignment())) {
+    // Unless this section is virtual (where we are allowed to adjust the offset
+    // freely), the padding goes in the previous section.
+    if (!IsVirtual) {
+      assert(SectionOrderIndex && "Invalid initial section address!");
+      MCSectionData *Prev = Layout.getSectionOrder()[SectionOrderIndex - 1];
+      Layout.setSectionFileSize(Prev, Layout.getSectionFileSize(Prev) + Pad);
     }
 
-    Layout.setFragmentEffectiveSize(&F, EffectiveSize);
-    Address += EffectiveSize;
+    StartAddress += Pad;
   }
 
-  // Set the section sizes.
-  Layout.setSectionSize(&SD, Address - StartAddress);
-  if (IsVirtual)
-    Layout.setSectionFileSize(&SD, 0);
-  else
-    Layout.setSectionFileSize(&SD, Address - StartAddress);
+  // Set the aligned section address.
+  Layout.setSectionAddress(&SD, StartAddress);
+
+  for (MCSectionData::iterator it = SD.begin(), ie = SD.end(); it != ie; ++it)
+    LayoutFragment(Layout, *it);
 
-  return Address;
+  // Set the section sizes.
+  uint64_t Size = 0;
+  if (!SD.getFragmentList().empty()) {
+    MCFragment *F = &SD.getFragmentList().back();
+    Size = Layout.getFragmentOffset(F) + Layout.getFragmentEffectiveSize(F);
+  }
+  Layout.setSectionSize(&SD, Size);
+  Layout.setSectionFileSize(&SD, IsVirtual ? 0 : Size);
 }
 
 /// WriteFragmentData - Write the \arg F data to the output file.
@@ -488,6 +483,8 @@ static void WriteFragmentData(const MCAssembler &Asm, const MCAsmLayout &Layout,
     MCAlignFragment &AF = cast<MCAlignFragment>(F);
     uint64_t Count = FragmentSize / AF.getValueSize();
 
+    assert(AF.getValueSize() && "Invalid virtual align in concrete fragment!");
+
     // FIXME: This error shouldn't actually occur (the front end should emit
     // multiple .align directives to enforce the semantics it wants), but is
     // severe enough that we want to report it. How to handle this?
@@ -531,7 +528,10 @@ static void WriteFragmentData(const MCAssembler &Asm, const MCAsmLayout &Layout,
 
   case MCFragment::FT_Fill: {
     MCFillFragment &FF = cast<MCFillFragment>(F);
-    for (uint64_t i = 0, e = FF.getCount(); i != e; ++i) {
+
+    assert(FF.getValueSize() && "Invalid virtual align in concrete fragment!");
+
+    for (uint64_t i = 0, e = FF.getSize() / FF.getValueSize(); i != e; ++i) {
       switch (FF.getValueSize()) {
       default:
         assert(0 && "Invalid size!");
@@ -556,11 +556,6 @@ static void WriteFragmentData(const MCAssembler &Asm, const MCAsmLayout &Layout,
 
     break;
   }
-
-  case MCFragment::FT_ZeroFill: {
-    assert(0 && "Invalid zero fill fragment in concrete section!");
-    break;
-  }
   }
 
   assert(OW->getStream().tell() - Start == FragmentSize);
@@ -575,6 +570,24 @@ void MCAssembler::WriteSectionData(const MCSectionData *SD,
   // Ignore virtual sections.
   if (getBackend().isVirtualSection(SD->getSection())) {
     assert(SectionFileSize == 0 && "Invalid size for section!");
+
+    // Check that contents are only things legal inside a virtual section.
+    for (MCSectionData::const_iterator it = SD->begin(),
+           ie = SD->end(); it != ie; ++it) {
+      switch (it->getKind()) {
+      default:
+        assert(0 && "Invalid fragment in virtual section!");
+      case MCFragment::FT_Align:
+        assert(!cast<MCAlignFragment>(it)->getValueSize() &&
+               "Invalid align in virtual section!");
+        break;
+      case MCFragment::FT_Fill:
+        assert(!cast<MCFillFragment>(it)->getValueSize() &&
+               "Invalid fill in virtual section!");
+        break;
+      }
+    }
+
     return;
   }
 
@@ -705,13 +718,9 @@ bool MCAssembler::FragmentNeedsRelaxation(const MCInstFragment *IF,
 bool MCAssembler::LayoutOnce(MCAsmLayout &Layout) {
   ++stats::RelaxationSteps;
 
-  // Layout the concrete sections and fragments.
-  uint64_t Address = 0;
-  for (MCAsmLayout::iterator it = Layout.begin(),
-         ie = Layout.end(); it != ie; ++it) {
-    // Layout the section fragments and its size.
-    Address = LayoutSection(**it, Layout, Address);
-  }
+  // Layout the sections in order.
+  for (unsigned i = 0, e = Layout.getSectionOrder().size(); i != e; ++i)
+    LayoutSection(Layout, i);
 
   // Scan for fragments that need relaxation.
   bool WasRelaxed = false;
@@ -877,7 +886,7 @@ void MCFillFragment::dump() {
   this->MCFragment::dump();
   OS << "\n       ";
   OS << " Value:" << getValue() << " ValueSize:" << getValueSize()
-     << " Count:" << getCount() << ">";
+     << " Size:" << getSize() << ">";
 }
 
 void MCInstFragment::dump() {
@@ -900,15 +909,6 @@ void MCOrgFragment::dump() {
   OS << " Offset:" << getOffset() << " Value:" << getValue() << ">";
 }
 
-void MCZeroFillFragment::dump() {
-  raw_ostream &OS = llvm::errs();
-
-  OS << "<MCZeroFillFragment ";
-  this->MCFragment::dump();
-  OS << "\n       ";
-  OS << " Size:" << getSize() << " Alignment:" << getAlignment() << ">";
-}
-
 void MCSectionData::dump() {
   raw_ostream &OS = llvm::errs();