-static void LowerInstFragment(MCInstFragment *IF,
- MCDataFragment *DF) {
-
- uint64_t DataOffset = DF->getContents().size();
-
- // Copy in the data
- DF->getContents().append(IF->getCode().begin(), IF->getCode().end());
-
- // Adjust the fixup offsets and add them to the data fragment.
- for (unsigned i = 0, e = IF->getFixups().size(); i != e; ++i) {
- MCFixup &F = IF->getFixups()[i];
- F.setOffset(DataOffset + F.getOffset());
- DF->getFixups().push_back(F);
- }
-}
-
-void MCAssembler::FinishLayout(MCAsmLayout &Layout) {
- // Lower out any instruction fragments, to simplify the fixup application and
- // output.
- //
- // FIXME-PERF: We don't have to do this, but the assumption is that it is
- // cheap (we will mostly end up eliminating fragments and appending on to data
- // fragments), so the extra complexity downstream isn't worth it. Evaluate
- // this assumption.
- unsigned FragmentIndex = 0;
- for (unsigned i = 0, e = Layout.getSectionOrder().size(); i != e; ++i) {
- MCSectionData &SD = *Layout.getSectionOrder()[i];
- MCDataFragment *CurDF = NULL;
-
- for (MCSectionData::iterator it2 = SD.begin(),
- ie2 = SD.end(); it2 != ie2; ++it2) {
- switch (it2->getKind()) {
- default:
- CurDF = NULL;
- break;
- case MCFragment::FT_Data:
- CurDF = cast<MCDataFragment>(it2);
- break;
- case MCFragment::FT_Inst: {
- MCInstFragment *IF = cast<MCInstFragment>(it2);
- // Use the existing data fragment if possible.
- if (CurDF && CurDF->getAtom() == IF->getAtom()) {
- Layout.CoalesceFragments(IF, CurDF);
- } else {
- // Otherwise, create a new data fragment.
- CurDF = new MCDataFragment();
- Layout.ReplaceFragment(IF, CurDF);
- }
-
- // Lower the Instruction Fragment
- LowerInstFragment(IF, CurDF);
-
- // Delete the instruction fragment and update the iterator.
- delete IF;
- it2 = CurDF;
- break;
- }
- }
- // Since we may have merged fragments, fix the layout order.
- it2->setLayoutOrder(FragmentIndex++);
- }