blob: bc9f60300ae2ca4fd94e634210dfb1f0af3db1cf [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//===----------------------------------------------------------------------===//
Stephen Hines37b74a32014-11-26 18:48:20 -08009#include "mcld/LD/BranchIslandFactory.h"
Shih-wei Liao22add6f2012-12-15 17:21:00 -080010
Stephen Hines37b74a32014-11-26 18:48:20 -080011#include "mcld/Fragment/Fragment.h"
12#include "mcld/LD/LDSection.h"
13#include "mcld/LD/SectionData.h"
14#include "mcld/Module.h"
15
16namespace mcld {
Shih-wei Liao22add6f2012-12-15 17:21:00 -080017
18//===----------------------------------------------------------------------===//
19// BranchIslandFactory
20//===----------------------------------------------------------------------===//
21
22/// ctor
Stephen Hines0dea6bc2014-07-15 18:33:32 -070023/// @param pMaxFwdBranchRange - the max forward branch range of the target
24/// @param pMaxBwdBranchRange - the max backward branch range of the target
25/// @param pMaxIslandSize - the predefined value for the max size of a island
26BranchIslandFactory::BranchIslandFactory(int64_t pMaxFwdBranchRange,
27 int64_t pMaxBwdBranchRange,
28 size_t pMaxIslandSize)
Stephen Hines37b74a32014-11-26 18:48:20 -080029 : GCFactory<BranchIsland, 0>(1u), // magic number
Stephen Hines0dea6bc2014-07-15 18:33:32 -070030 m_MaxFwdBranchRange(pMaxFwdBranchRange - pMaxIslandSize),
31 m_MaxBwdBranchRange(pMaxBwdBranchRange + pMaxIslandSize),
Stephen Hines37b74a32014-11-26 18:48:20 -080032 m_MaxIslandSize(pMaxIslandSize) {
Shih-wei Liao22add6f2012-12-15 17:21:00 -080033}
34
Stephen Hines37b74a32014-11-26 18:48:20 -080035BranchIslandFactory::~BranchIslandFactory() {
Shih-wei Liao22add6f2012-12-15 17:21:00 -080036}
37
Stephen Hines87f34652014-02-14 18:00:16 -080038/// group - group fragments and create islands when needed
39/// @param pSectionData - the SectionData holds fragments need to be grouped
Stephen Hines37b74a32014-11-26 18:48:20 -080040void BranchIslandFactory::group(Module& pModule) {
Stephen Hinescfcb2242016-03-08 00:18:09 -080041 for (Module::iterator sect = pModule.begin(), sectEnd = pModule.end();
42 sect != sectEnd; ++sect) {
43 if (((*sect)->kind() == LDFileFormat::TEXT) && (*sect)->hasSectionData()) {
44 SectionData& sd = *((*sect)->getSectionData());
45 uint64_t group_end = m_MaxFwdBranchRange;
46 for (SectionData::iterator it = sd.begin(), ie = sd.end(); it != ie;
47 ++it) {
48 if ((*it).getOffset() + (*it).size() > group_end) {
49 Fragment* frag = (*it).getPrevNode();
50 while (frag != NULL && frag->getKind() == Fragment::Alignment) {
51 frag = frag->getPrevNode();
52 }
53 if (frag != NULL) {
54 produce(*frag);
55 group_end = (*it).getOffset() + m_MaxFwdBranchRange;
56 }
Stephen Hines87f34652014-02-14 18:00:16 -080057 }
58 }
Stephen Hinescfcb2242016-03-08 00:18:09 -080059 if (getIslands(sd.back()).first == NULL)
60 produce(sd.back());
Stephen Hines87f34652014-02-14 18:00:16 -080061 }
Stephen Hines87f34652014-02-14 18:00:16 -080062 }
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
Stephen Hines37b74a32014-11-26 18:48:20 -080067BranchIsland* BranchIslandFactory::produce(Fragment& pFragment) {
68 BranchIsland* island = allocate();
69 new (island) BranchIsland(pFragment, // entry fragment to the island
70 m_MaxIslandSize, // the max size of the island
71 size() - 1u); // index in the island factory
Shih-wei Liao22add6f2012-12-15 17:21:00 -080072 return island;
73}
74
Stephen Hines0dea6bc2014-07-15 18:33:32 -070075/// getIsland - find fwd and bwd islands for the fragment
76/// @param pFragment - the fragment needs a branch island
Stephen Hines37b74a32014-11-26 18:48:20 -080077std::pair<BranchIsland*, BranchIsland*> BranchIslandFactory::getIslands(
78 const Fragment& pFragment) {
Stephen Hines0dea6bc2014-07-15 18:33:32 -070079 BranchIsland* fwd = NULL;
80 BranchIsland* bwd = NULL;
81 for (iterator it = begin(), ie = end(), prev = ie; it != ie;
82 prev = it, ++it) {
Stephen Hinescfcb2242016-03-08 00:18:09 -080083 if (pFragment.getParent() != (*it).getParent()) {
84 continue;
85 }
86
Shih-wei Liao22add6f2012-12-15 17:21:00 -080087 if ((pFragment.getOffset() < (*it).offset()) &&
Stephen Hines0dea6bc2014-07-15 18:33:32 -070088 ((pFragment.getOffset() + m_MaxFwdBranchRange) >= (*it).offset())) {
Stephen Hines0dea6bc2014-07-15 18:33:32 -070089 fwd = &*it;
90
Stephen Hinescfcb2242016-03-08 00:18:09 -080091 if ((prev != ie) && (pFragment.getParent() == (*prev).getParent())) {
Stephen Hines0dea6bc2014-07-15 18:33:32 -070092 int64_t bwd_off = (int64_t)pFragment.getOffset() + m_MaxBwdBranchRange;
93 if ((pFragment.getOffset() > (*prev).offset()) &&
Stephen Hines37b74a32014-11-26 18:48:20 -080094 (bwd_off <= (int64_t)(*prev).offset())) {
Stephen Hines0dea6bc2014-07-15 18:33:32 -070095 bwd = &*prev;
96 }
97 }
98 break;
99 }
100 }
101 return std::make_pair(fwd, bwd);
102}
Stephen Hines37b74a32014-11-26 18:48:20 -0800103
104} // namespace mcld