|
15 | 15 | #include "helpers.hpp" |
16 | 16 | #include "itertools.hpp" |
17 | 17 | #include "linkdefs.hpp" |
18 | | -#include "platform.hpp" // MUSTTAIL |
19 | 18 | #include "verbosity.hpp" |
20 | 19 |
|
21 | 20 | #include "link/main.hpp" |
@@ -119,92 +118,93 @@ static MemoryLocation getStartLocation(Section const §ion) { |
119 | 118 | static std::optional<size_t> getPlacement(Section const §ion, MemoryLocation &location) { |
120 | 119 | SectionTypeInfo const &typeInfo = sectionTypeInfo[section.type]; |
121 | 120 |
|
122 | | - // Switch to the beginning of the next bank |
123 | | - std::deque<FreeSpace> &bankMem = memory[section.type][location.bank - typeInfo.firstBank]; |
124 | | - size_t spaceIdx = 0; |
| 121 | + for (;;) { |
| 122 | + // Switch to the beginning of the next bank |
| 123 | + std::deque<FreeSpace> &bankMem = memory[section.type][location.bank - typeInfo.firstBank]; |
| 124 | + size_t spaceIdx = 0; |
125 | 125 |
|
126 | | - if (spaceIdx < bankMem.size()) { |
127 | | - location.address = bankMem[spaceIdx].address; |
128 | | - } |
129 | | - |
130 | | - // Process locations in that bank |
131 | | - while (spaceIdx < bankMem.size()) { |
132 | | - // If that location is OK, return it |
133 | | - if (isLocationSuitable(section, bankMem[spaceIdx], location)) { |
134 | | - return spaceIdx; |
| 126 | + if (spaceIdx < bankMem.size()) { |
| 127 | + location.address = bankMem[spaceIdx].address; |
135 | 128 | } |
136 | 129 |
|
137 | | - // Go to the next *possible* location |
138 | | - if (section.isAddressFixed) { |
139 | | - // If the address is fixed, there can be only one candidate block per bank; |
140 | | - // if we already reached it, give up. |
141 | | - if (location.address < section.org) { |
142 | | - location.address = section.org; |
143 | | - } else { |
144 | | - break; // Try again in next bank |
| 130 | + // Process locations in that bank |
| 131 | + while (spaceIdx < bankMem.size()) { |
| 132 | + // If that location is OK, return it |
| 133 | + if (isLocationSuitable(section, bankMem[spaceIdx], location)) { |
| 134 | + return spaceIdx; |
145 | 135 | } |
146 | | - } else if (section.isAlignFixed) { |
147 | | - // Move to next aligned location |
148 | | - // Move back to alignment boundary |
149 | | - location.address -= section.alignOfs; |
150 | | - // Ensure we're there (e.g. on first check) |
151 | | - location.address &= ~section.alignMask; |
152 | | - // Go to next align boundary and add offset |
153 | | - location.address += section.alignMask + 1 + section.alignOfs; |
154 | | - } else if (++spaceIdx < bankMem.size()) { |
155 | | - // Any location is fine, so, next free block |
156 | | - location.address = bankMem[spaceIdx].address; |
157 | | - } |
158 | 136 |
|
159 | | - // If that location is past the current block's end, |
160 | | - // go forwards until that is no longer the case. |
161 | | - while (spaceIdx < bankMem.size() |
162 | | - && location.address >= bankMem[spaceIdx].address + bankMem[spaceIdx].size) { |
163 | | - ++spaceIdx; |
164 | | - } |
| 137 | + // Go to the next *possible* location |
| 138 | + if (section.isAddressFixed) { |
| 139 | + // If the address is fixed, there can be only one candidate block per bank; |
| 140 | + // if we already reached it, give up and try again in the next bank. |
| 141 | + if (location.address >= section.org) { |
| 142 | + break; |
| 143 | + } |
| 144 | + location.address = section.org; |
| 145 | + } else if (section.isAlignFixed) { |
| 146 | + // Move to next aligned location |
| 147 | + // Move back to alignment boundary |
| 148 | + location.address -= section.alignOfs; |
| 149 | + // Ensure we're there (e.g. on first check) |
| 150 | + location.address &= ~section.alignMask; |
| 151 | + // Go to next align boundary and add offset |
| 152 | + location.address += section.alignMask + 1 + section.alignOfs; |
| 153 | + } else if (++spaceIdx < bankMem.size()) { |
| 154 | + // Any location is fine, so, next free block |
| 155 | + location.address = bankMem[spaceIdx].address; |
| 156 | + } |
165 | 157 |
|
166 | | - // Try again with the new location/free space combo |
167 | | - } |
| 158 | + // If that location is past the current block's end, |
| 159 | + // go forwards until that is no longer the case. |
| 160 | + while (spaceIdx < bankMem.size() |
| 161 | + && location.address >= bankMem[spaceIdx].address + bankMem[spaceIdx].size) { |
| 162 | + ++spaceIdx; |
| 163 | + } |
168 | 164 |
|
169 | | - // Try again in the next bank, if one is available. |
170 | | - // Try scrambled banks in descending order until no bank in the scrambled range is |
171 | | - // available. Otherwise, try in ascending order. |
172 | | - if (section.isBankFixed) { |
173 | | - return std::nullopt; |
174 | | - } else if (options.scrambleROMX && section.type == SECTTYPE_ROMX |
175 | | - && location.bank <= options.scrambleROMX) { |
176 | | - if (location.bank > typeInfo.firstBank) { |
177 | | - --location.bank; |
178 | | - } else if (options.scrambleROMX < typeInfo.lastBank) { |
179 | | - location.bank = options.scrambleROMX + 1; |
180 | | - } else { |
181 | | - return std::nullopt; |
| 165 | + // Try again with the new location/free space combo |
182 | 166 | } |
183 | | - } else if (options.scrambleWRAMX && section.type == SECTTYPE_WRAMX |
184 | | - && location.bank <= options.scrambleWRAMX) { |
185 | | - if (location.bank > typeInfo.firstBank) { |
186 | | - --location.bank; |
187 | | - } else if (options.scrambleWRAMX < typeInfo.lastBank) { |
188 | | - location.bank = options.scrambleWRAMX + 1; |
189 | | - } else { |
| 167 | + |
| 168 | + // Try again in the next bank, if one is available. |
| 169 | + // Try scrambled banks in descending order until no bank in the scrambled range is |
| 170 | + // available. Otherwise, try in ascending order. |
| 171 | + if (section.isBankFixed) { |
190 | 172 | return std::nullopt; |
191 | | - } |
192 | | - } else if (options.scrambleSRAM && section.type == SECTTYPE_SRAM |
193 | | - && location.bank <= options.scrambleSRAM) { |
194 | | - if (location.bank > typeInfo.firstBank) { |
195 | | - --location.bank; |
196 | | - } else if (options.scrambleSRAM < typeInfo.lastBank) { |
197 | | - location.bank = options.scrambleSRAM + 1; |
| 173 | + } else if (options.scrambleROMX && section.type == SECTTYPE_ROMX |
| 174 | + && location.bank <= options.scrambleROMX) { |
| 175 | + if (location.bank > typeInfo.firstBank) { |
| 176 | + --location.bank; |
| 177 | + } else if (options.scrambleROMX < typeInfo.lastBank) { |
| 178 | + location.bank = options.scrambleROMX + 1; |
| 179 | + } else { |
| 180 | + return std::nullopt; |
| 181 | + } |
| 182 | + } else if (options.scrambleWRAMX && section.type == SECTTYPE_WRAMX |
| 183 | + && location.bank <= options.scrambleWRAMX) { |
| 184 | + if (location.bank > typeInfo.firstBank) { |
| 185 | + --location.bank; |
| 186 | + } else if (options.scrambleWRAMX < typeInfo.lastBank) { |
| 187 | + location.bank = options.scrambleWRAMX + 1; |
| 188 | + } else { |
| 189 | + return std::nullopt; |
| 190 | + } |
| 191 | + } else if (options.scrambleSRAM && section.type == SECTTYPE_SRAM |
| 192 | + && location.bank <= options.scrambleSRAM) { |
| 193 | + if (location.bank > typeInfo.firstBank) { |
| 194 | + --location.bank; |
| 195 | + } else if (options.scrambleSRAM < typeInfo.lastBank) { |
| 196 | + location.bank = options.scrambleSRAM + 1; |
| 197 | + } else { |
| 198 | + return std::nullopt; |
| 199 | + } |
| 200 | + } else if (location.bank < typeInfo.lastBank) { |
| 201 | + ++location.bank; |
198 | 202 | } else { |
199 | 203 | return std::nullopt; |
200 | 204 | } |
201 | | - } else if (location.bank < typeInfo.lastBank) { |
202 | | - ++location.bank; |
203 | | - } else { |
204 | | - return std::nullopt; |
205 | | - } |
206 | 205 |
|
207 | | - MUSTTAIL return getPlacement(section, location); |
| 206 | + // Try again in the next iteration. |
| 207 | + } |
208 | 208 | } |
209 | 209 |
|
210 | 210 | static std::string getSectionDescription(Section const §ion) { |
|
0 commit comments