BranchIslandFactory.cpp revision 48b4262286b854f4fa36d2af98cf8d75e87d4dcb
1//===- BranchIslandFactory.cpp --------------------------------------------===//
2//
3//                     The MCLinker Project
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9#include <mcld/LD/BranchIslandFactory.h>
10#include <mcld/Fragment/Fragment.h>
11#include <mcld/LD/LDSection.h>
12#include <mcld/LD/SectionData.h>
13#include <mcld/Module.h>
14
15using namespace mcld;
16
17//===----------------------------------------------------------------------===//
18// BranchIslandFactory
19//===----------------------------------------------------------------------===//
20
21/// ctor
22/// @param pMaxFwdBranchRange - the max forward branch range of the target
23/// @param pMaxBwdBranchRange - the max backward branch range of the target
24/// @param pMaxIslandSize - the predefined value for the max size of a island
25BranchIslandFactory::BranchIslandFactory(int64_t pMaxFwdBranchRange,
26                                         int64_t pMaxBwdBranchRange,
27                                         size_t pMaxIslandSize)
28    : GCFactory<BranchIsland, 0>(1u), // magic number
29      m_MaxFwdBranchRange(pMaxFwdBranchRange - pMaxIslandSize),
30      m_MaxBwdBranchRange(pMaxBwdBranchRange + pMaxIslandSize),
31      m_MaxIslandSize(pMaxIslandSize)
32{
33}
34
35BranchIslandFactory::~BranchIslandFactory()
36{
37}
38
39/// group - group fragments and create islands when needed
40/// @param pSectionData - the SectionData holds fragments need to be grouped
41void BranchIslandFactory::group(Module& pModule)
42{
43  /* FIXME: Currently only support relaxing .text section! */
44  LDSection* text = pModule.getSection(".text");
45  if (text != NULL && text->hasSectionData()) {
46    SectionData& sd = *text->getSectionData();
47    uint64_t group_end = m_MaxFwdBranchRange;
48    for (SectionData::iterator it = sd.begin(), ie = sd.end(); it != ie; ++it) {
49      if ((*it).getOffset() + (*it).size() > group_end) {
50        Fragment* frag = (*it).getPrevNode();
51        while (frag != NULL && frag->getKind() == Fragment::Alignment) {
52          frag = frag->getPrevNode();
53        }
54        if (frag != NULL) {
55          produce(*frag);
56          group_end = (*it).getOffset() + m_MaxFwdBranchRange;
57        }
58      }
59    }
60    if (getIslands(sd.back()).first == NULL)
61      produce(sd.back());
62  }
63}
64
65/// produce - produce a island for the given fragment
66/// @param pFragment - the fragment needs a branch island
67BranchIsland* BranchIslandFactory::produce(Fragment& pFragment)
68{
69  BranchIsland *island = allocate();
70  new (island) BranchIsland(pFragment,       // entry fragment to the island
71                            m_MaxIslandSize, // the max size of the island
72                            size() - 1u);    // index in the island factory
73  return island;
74}
75
76/// getIsland - find fwd and bwd islands for the fragment
77/// @param pFragment - the fragment needs a branch island
78std::pair<BranchIsland*, BranchIsland*>
79BranchIslandFactory::getIslands(const Fragment& pFragment)
80{
81  BranchIsland* fwd = NULL;
82  BranchIsland* bwd = NULL;
83  for (iterator it = begin(), ie = end(), prev = ie; it != ie;
84       prev = it, ++it) {
85    if ((pFragment.getOffset() < (*it).offset()) &&
86        ((pFragment.getOffset() + m_MaxFwdBranchRange) >= (*it).offset())) {
87
88      fwd = &*it;
89
90      if (prev != ie) {
91        int64_t bwd_off = (int64_t)pFragment.getOffset() + m_MaxBwdBranchRange;
92        if ((pFragment.getOffset() > (*prev).offset()) &&
93            (bwd_off <= (int64_t) (*prev).offset())) {
94          bwd = &*prev;
95        }
96      }
97      break;
98    }
99  }
100  return std::make_pair(fwd, bwd);
101}
102