Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 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 | //===----------------------------------------------------------------------===// |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 9 | #include "mcld/LD/BranchIslandFactory.h" |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 10 | |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 11 | #include "mcld/Fragment/Fragment.h" |
| 12 | #include "mcld/LD/LDSection.h" |
| 13 | #include "mcld/LD/SectionData.h" |
| 14 | #include "mcld/Module.h" |
| 15 | |
| 16 | namespace mcld { |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 17 | |
| 18 | //===----------------------------------------------------------------------===// |
| 19 | // BranchIslandFactory |
| 20 | //===----------------------------------------------------------------------===// |
| 21 | |
| 22 | /// ctor |
Stephen Hines | 0dea6bc | 2014-07-15 18:33:32 -0700 | [diff] [blame] | 23 | /// @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 |
| 26 | BranchIslandFactory::BranchIslandFactory(int64_t pMaxFwdBranchRange, |
| 27 | int64_t pMaxBwdBranchRange, |
| 28 | size_t pMaxIslandSize) |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 29 | : GCFactory<BranchIsland, 0>(1u), // magic number |
Stephen Hines | 0dea6bc | 2014-07-15 18:33:32 -0700 | [diff] [blame] | 30 | m_MaxFwdBranchRange(pMaxFwdBranchRange - pMaxIslandSize), |
| 31 | m_MaxBwdBranchRange(pMaxBwdBranchRange + pMaxIslandSize), |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 32 | m_MaxIslandSize(pMaxIslandSize) { |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 33 | } |
| 34 | |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 35 | BranchIslandFactory::~BranchIslandFactory() { |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 36 | } |
| 37 | |
Stephen Hines | 87f3465 | 2014-02-14 18:00:16 -0800 | [diff] [blame] | 38 | /// group - group fragments and create islands when needed |
| 39 | /// @param pSectionData - the SectionData holds fragments need to be grouped |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 40 | void BranchIslandFactory::group(Module& pModule) { |
Stephen Hines | cfcb224 | 2016-03-08 00:18:09 -0800 | [diff] [blame^] | 41 | 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 Hines | 87f3465 | 2014-02-14 18:00:16 -0800 | [diff] [blame] | 57 | } |
| 58 | } |
Stephen Hines | cfcb224 | 2016-03-08 00:18:09 -0800 | [diff] [blame^] | 59 | if (getIslands(sd.back()).first == NULL) |
| 60 | produce(sd.back()); |
Stephen Hines | 87f3465 | 2014-02-14 18:00:16 -0800 | [diff] [blame] | 61 | } |
Stephen Hines | 87f3465 | 2014-02-14 18:00:16 -0800 | [diff] [blame] | 62 | } |
| 63 | } |
| 64 | |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 65 | /// produce - produce a island for the given fragment |
| 66 | /// @param pFragment - the fragment needs a branch island |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 67 | BranchIsland* 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 Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 72 | return island; |
| 73 | } |
| 74 | |
Stephen Hines | 0dea6bc | 2014-07-15 18:33:32 -0700 | [diff] [blame] | 75 | /// getIsland - find fwd and bwd islands for the fragment |
| 76 | /// @param pFragment - the fragment needs a branch island |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 77 | std::pair<BranchIsland*, BranchIsland*> BranchIslandFactory::getIslands( |
| 78 | const Fragment& pFragment) { |
Stephen Hines | 0dea6bc | 2014-07-15 18:33:32 -0700 | [diff] [blame] | 79 | BranchIsland* fwd = NULL; |
| 80 | BranchIsland* bwd = NULL; |
| 81 | for (iterator it = begin(), ie = end(), prev = ie; it != ie; |
| 82 | prev = it, ++it) { |
Stephen Hines | cfcb224 | 2016-03-08 00:18:09 -0800 | [diff] [blame^] | 83 | if (pFragment.getParent() != (*it).getParent()) { |
| 84 | continue; |
| 85 | } |
| 86 | |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 87 | if ((pFragment.getOffset() < (*it).offset()) && |
Stephen Hines | 0dea6bc | 2014-07-15 18:33:32 -0700 | [diff] [blame] | 88 | ((pFragment.getOffset() + m_MaxFwdBranchRange) >= (*it).offset())) { |
Stephen Hines | 0dea6bc | 2014-07-15 18:33:32 -0700 | [diff] [blame] | 89 | fwd = &*it; |
| 90 | |
Stephen Hines | cfcb224 | 2016-03-08 00:18:09 -0800 | [diff] [blame^] | 91 | if ((prev != ie) && (pFragment.getParent() == (*prev).getParent())) { |
Stephen Hines | 0dea6bc | 2014-07-15 18:33:32 -0700 | [diff] [blame] | 92 | int64_t bwd_off = (int64_t)pFragment.getOffset() + m_MaxBwdBranchRange; |
| 93 | if ((pFragment.getOffset() > (*prev).offset()) && |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 94 | (bwd_off <= (int64_t)(*prev).offset())) { |
Stephen Hines | 0dea6bc | 2014-07-15 18:33:32 -0700 | [diff] [blame] | 95 | bwd = &*prev; |
| 96 | } |
| 97 | } |
| 98 | break; |
| 99 | } |
| 100 | } |
| 101 | return std::make_pair(fwd, bwd); |
| 102 | } |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 103 | |
| 104 | } // namespace mcld |