blob: 0147e5fdd1bf4a7ba36c5f3b3acbb7455eefffe1 [file] [log] [blame]
Shih-wei Liao22add6f2012-12-15 17:21:00 -08001//===- 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>
Stephen Hinesf33f6de2014-02-14 18:00:16 -080011#include <mcld/LD/LDSection.h>
12#include <mcld/LD/SectionData.h>
13#include <mcld/Module.h>
Shih-wei Liao22add6f2012-12-15 17:21:00 -080014
15using namespace mcld;
16
17//===----------------------------------------------------------------------===//
18// BranchIslandFactory
19//===----------------------------------------------------------------------===//
20
21/// ctor
Stephen Hinesa790f0a2014-07-15 18:33:32 -070022/// @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)
Shih-wei Liao22add6f2012-12-15 17:21:00 -080032{
33}
34
35BranchIslandFactory::~BranchIslandFactory()
36{
37}
38
Stephen Hinesf33f6de2014-02-14 18:00:16 -080039/// 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{
Stephen Hinesa790f0a2014-07-15 18:33:32 -070043 /* FIXME: Currently only support relaxing .text section! */
Stephen Hinesf33f6de2014-02-14 18:00:16 -080044 LDSection* text = pModule.getSection(".text");
45 if (text != NULL && text->hasSectionData()) {
46 SectionData& sd = *text->getSectionData();
Stephen Hinesa790f0a2014-07-15 18:33:32 -070047 uint64_t group_end = m_MaxFwdBranchRange;
Stephen Hinesf33f6de2014-02-14 18:00:16 -080048 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);
Stephen Hinesa790f0a2014-07-15 18:33:32 -070056 group_end = (*it).getOffset() + m_MaxFwdBranchRange;
Stephen Hinesf33f6de2014-02-14 18:00:16 -080057 }
58 }
59 }
Stephen Hinesa790f0a2014-07-15 18:33:32 -070060 if (getIslands(sd.back()).first == NULL)
Stephen Hinesf33f6de2014-02-14 18:00:16 -080061 produce(sd.back());
62 }
63}
64
Shih-wei Liao22add6f2012-12-15 17:21:00 -080065/// produce - produce a island for the given fragment
66/// @param pFragment - the fragment needs a branch island
67BranchIsland* BranchIslandFactory::produce(Fragment& pFragment)
68{
Shih-wei Liao22add6f2012-12-15 17:21:00 -080069 BranchIsland *island = allocate();
Stephen Hinesf33f6de2014-02-14 18:00:16 -080070 new (island) BranchIsland(pFragment, // entry fragment to the island
Shih-wei Liao22add6f2012-12-15 17:21:00 -080071 m_MaxIslandSize, // the max size of the island
Stephen Hinesf33f6de2014-02-14 18:00:16 -080072 size() - 1u); // index in the island factory
Shih-wei Liao22add6f2012-12-15 17:21:00 -080073 return island;
74}
75
Stephen Hinesa790f0a2014-07-15 18:33:32 -070076/// 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)
Shih-wei Liao22add6f2012-12-15 17:21:00 -080080{
Stephen Hinesa790f0a2014-07-15 18:33:32 -070081 BranchIsland* fwd = NULL;
82 BranchIsland* bwd = NULL;
83 for (iterator it = begin(), ie = end(), prev = ie; it != ie;
84 prev = it, ++it) {
Shih-wei Liao22add6f2012-12-15 17:21:00 -080085 if ((pFragment.getOffset() < (*it).offset()) &&
Stephen Hinesa790f0a2014-07-15 18:33:32 -070086 ((pFragment.getOffset() + m_MaxFwdBranchRange) >= (*it).offset())) {
Shih-wei Liao22add6f2012-12-15 17:21:00 -080087
Stephen Hinesa790f0a2014-07-15 18:33:32 -070088 fwd = &*it;
89
90 if (prev != ie) {
91 int64_t bwd_off = (int64_t)pFragment.getOffset() + m_MaxBwdBranchRange;
92 if ((pFragment.getOffset() > (*prev).offset()) &&
Stephen Hines48b42622014-07-15 18:36:27 -070093 (bwd_off <= (int64_t) (*prev).offset())) {
Stephen Hinesa790f0a2014-07-15 18:33:32 -070094 bwd = &*prev;
95 }
96 }
97 break;
98 }
99 }
100 return std::make_pair(fwd, bwd);
101}