fix_m68k.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825
  1. /* fix_m68k.c: Routines for M68000 code fixup
  2. Copyright (C) 2003 Sebastian Reichelt
  3. Copyright (C) 2003-2004 Kevin Kofler
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software Foundation,
  14. Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  15. #include "fix_m68k.h"
  16. #include "m68k.h"
  17. #include "fix_tios.h"
  18. #include "fix_emu.h"
  19. #include "cutrange.h"
  20. #include "../manip.h"
  21. #include <stdlib.h>
  22. // Apply generic code fixes and optimizations to a section.
  23. void M68kFixCode (SECTION *Section)
  24. {
  25. M68kFixCodePreMerge (Section, NULL, 0);
  26. }
  27. // Fix and optionally optimize code executable on the M68k processor family,
  28. // for two sections which are to be merged.
  29. // Src may be NULL.
  30. // If DestSize is nonzero, it specifies a fixed size for the destination
  31. // section, which will not change even when cutting ranges from it.
  32. void M68kFixCodePreMerge (SECTION *Dest, SECTION *Src, SIZE DestSize)
  33. {
  34. if (Dest->Code && (!(Dest->Frozen)) && (!(Dest->Parent->Frozen)))
  35. {
  36. OPTIMIZE_INFO *OptimizeInfo = Dest->Parent->OptimizeInfo;
  37. SIZE OrigSize = Dest->Size;
  38. RELOC *Reloc, *NextReloc;
  39. // For each reloc...
  40. for (Reloc = (DestSize ? GetFirst (Dest->Relocs) : GetLast (Dest->Relocs)); Reloc; Reloc = NextReloc)
  41. {
  42. NextReloc = (DestSize ? GetNext (Reloc) : GetPrev (Reloc));
  43. // Completely ignore builtin relocs. Also ignore relation-relative
  44. // relocs, since the only relative relocs we can optimize further
  45. // are branches, and those are never relation-relative.
  46. // Ignore relocs which are not in code segments.
  47. if ((!(Reloc->Target.Builtin || Reloc->Relation || Reloc->Unoptimizable)) && IsCodeRange (Dest, Reloc->Location, Reloc->Location + Reloc->Size))
  48. {
  49. // We can only fix or optimize a reloc whose target we know.
  50. if (Reloc->Target.Symbol && ((Reloc->Target.Symbol->Parent == Dest) || (Reloc->Target.Symbol->Parent == Src)))
  51. {
  52. OFFSET TargetDistance;
  53. // Get the distance of the target symbol, provided it is in the
  54. // same section.
  55. if (Reloc->Target.Symbol->Parent == Dest)
  56. TargetDistance = GetLocationOffset (Dest, &(Reloc->Target));
  57. // If it is in the next section, add the section size.
  58. // This is not 100% correct, since we might pad the section, but
  59. // currently there is no reason to add padding to code. If we get
  60. // a slightly wrong distance, we will probably get away with it
  61. // anyway.
  62. else
  63. TargetDistance = (DestSize ? : Dest->Size) + GetLocationOffset (Src, &(Reloc->Target));
  64. // Add the fixed offset because it needs to be added to the reloc
  65. // target. Subtract the location of the reloc.
  66. TargetDistance += Reloc->FixedOffset - Reloc->Location;
  67. // Fix and possibly optimize or remove the reloc.
  68. M68kFixReloc (Reloc, TargetDistance, OptimizeInfo);
  69. }
  70. }
  71. }
  72. if (Dest->Size < OrigSize)
  73. FinalizeRangeCutting (Dest);
  74. }
  75. }
  76. // Cut the range between Start and End (not including End) if that is
  77. // permitted; otherwise fill it with NOPs. Start and End are expected
  78. // to be at even addresses. It is also assumed that IsBinaryDataRange
  79. // has been called on the range (without an exception, or with an
  80. // exception that has since been removed).
  81. void M68kCutOrFillRange (SECTION *Section, OFFSET Start, OFFSET End, OPTIMIZE_INFO *OptimizeInfo)
  82. {
  83. if (End > Start)
  84. {
  85. BOOLEAN SectionEnd = End >= Section->Size;
  86. // Make sure Start is not outside of the section data;
  87. if (Start < 0)
  88. Start = 0;
  89. // Make sure End is not outside of the section data;
  90. if (SectionEnd)
  91. End = Section->Size;
  92. // If the range is at the end of the section, shrink the section.
  93. if (SectionEnd && CanShrinkSection (Section, Start, NULL))
  94. {
  95. // Look for a short branch to the end of the section. GNU as
  96. // does this to simulate long conditional branches.
  97. BOOLEAN BranchFound = FALSE;
  98. const I1 *Data = Section->Data;
  99. if (Data)
  100. {
  101. SIZE Size = Section->Size;
  102. OFFSET CurPos;
  103. for (CurPos = Size - 4; (CurPos >= 0) && (CurPos >= Size - 8); CurPos -= 2)
  104. {
  105. if (((Data [CurPos + 0] & M68K_Bcc_MASK_0) == M68K_Bcc_S_0) && (Data [CurPos + 1] == Size - (CurPos + 2)))
  106. {
  107. BranchFound = TRUE;
  108. break;
  109. }
  110. }
  111. }
  112. if (!BranchFound)
  113. {
  114. // No such branch was found, so we can (hopefully) safely cut
  115. // the section.
  116. CutSection (Section, Start);
  117. return;
  118. }
  119. }
  120. // If the range is in the middle of the section, try to cut it out.
  121. else
  122. {
  123. if (CanCutRange (Section, Start, End))
  124. {
  125. SIZE Length = End - Start;
  126. OptimizeInfo->CutRangesResult += Length;
  127. if (OptimizeInfo->CutRanges)
  128. {
  129. CutRange (Section, Start, End);
  130. OptimizeInfo->NearAssemblyResult -= Length;
  131. return;
  132. }
  133. }
  134. }
  135. // If we cannot cut the range for some reason, fill it with NOPs.
  136. {
  137. I1 *Data = Section->Data;
  138. if (Data)
  139. {
  140. OFFSET CurPos;
  141. for (CurPos = Start; CurPos + 1 < End; CurPos += 2)
  142. {
  143. Data [CurPos + 0] = M68K_NOP_0;
  144. Data [CurPos + 1] = M68K_NOP_1;
  145. }
  146. }
  147. }
  148. }
  149. }
  150. // Fix and possibly optimize a given relocation entry.
  151. // TargetDistance is the estimated (signed) distance of the relocation target, including the
  152. // fixed offset. OptimizeInfo contains information about what to optimize.
  153. // The reloc may be removed during the process.
  154. void M68kFixReloc (RELOC *Reloc, OFFSET TargetDistance, OPTIMIZE_INFO *OptimizeInfo)
  155. {
  156. SECTION *Section = Reloc->Parent;
  157. I1 *Data = Section->Data;
  158. OFFSET RelocLocation = Reloc->Location;
  159. OFFSET OpcodeLocation;
  160. I1 *Opcode;
  161. if (!Data)
  162. return;
  163. if (Reloc->Unoptimizable)
  164. return;
  165. // Be careful with relocs at the beginning of the section; they might
  166. // cause segmentation faults when we try to determine the opcode.
  167. // Usually, ignore all relocs whose location is less than 2.
  168. // As a special exception, allow 1-byte relocs with a location of 1.
  169. if (!((Reloc->Location >= 2) || ((Reloc->Size == 1) && (Reloc->Location == 1))))
  170. return;
  171. // Optimize a subroutine branch followed by an RTS.
  172. if (OptimizeInfo->OptimizeReturns)
  173. {
  174. OFFSET RelocEnd = RelocLocation + Reloc->Size;
  175. OFFSET RTSEnd = RelocEnd + 2;
  176. if ((RTSEnd <= Section->Size) && (Data [RelocEnd + 0] == M68K_RTS_0) && (Data [RelocEnd + 1] == M68K_RTS_1))
  177. {
  178. OFFSET OpcodeLocation;
  179. I1 *Opcode;
  180. BOOLEAN Optimized = FALSE;
  181. if (Reloc->Relative)
  182. {
  183. switch (Reloc->Size)
  184. {
  185. // Optimize 1-byte relative relocs.
  186. case 1:
  187. OpcodeLocation = RelocLocation - 1;
  188. Opcode = Data + OpcodeLocation;
  189. // Check if the reloc belongs to a BSR.S, and that there
  190. // is nothing in our way.
  191. if ((Opcode [0] == M68K_BSR_S_0) && (IsBinaryDataRange (Section, OpcodeLocation, RTSEnd, Reloc)))
  192. {
  193. // Optimize it into a BRA.S.
  194. Opcode [0] = M68K_BRA_S_0;
  195. Optimized = TRUE;
  196. }
  197. break;
  198. // Optimize 2-byte relative relocs.
  199. case 2:
  200. OpcodeLocation = RelocLocation - 2;
  201. Opcode = Data + OpcodeLocation;
  202. // Check if the reloc belongs to a BSR.W, and that there
  203. // is nothing in our way.
  204. if ((Opcode [0] == M68K_BSR_W_0) && (Opcode [1] == M68K_BSR_W_1) && (IsBinaryDataRange (Section, OpcodeLocation, RTSEnd, Reloc)))
  205. {
  206. // Optimize it into a JMP.
  207. Opcode [0] = M68K_BRA_W_0;
  208. Opcode [1] = M68K_BRA_W_1;
  209. Optimized = TRUE;
  210. }
  211. break;
  212. }
  213. }
  214. else
  215. {
  216. // Optimize 4-byte absolute relocs.
  217. if (Reloc->Size == 4)
  218. {
  219. OpcodeLocation = RelocLocation - 2;
  220. Opcode = Data + OpcodeLocation;
  221. // Check if the reloc belongs to a JSR, and that there is
  222. // nothing in our way.
  223. if ((Opcode [0] == M68K_JSR_0) && (Opcode [1] == M68K_JSR_1) && (IsBinaryDataRange (Section, OpcodeLocation, RTSEnd, Reloc)))
  224. {
  225. // Optimize it into a JMP.
  226. Opcode [0] = M68K_JMP_0;
  227. Opcode [1] = M68K_JMP_1;
  228. Optimized = TRUE;
  229. }
  230. }
  231. }
  232. // Remove the RTS.
  233. if (Optimized)
  234. M68kCutOrFillRange (Section, RelocEnd, RTSEnd, OptimizeInfo);
  235. }
  236. }
  237. // Only branch fixes and optimizations make sense for relative relocs.
  238. if (Reloc->Relative)
  239. {
  240. // Check if it is a 1-byte relative reloc at the end of the
  241. // section, pointing to the next instruction. This means that
  242. // the value will probably be 0, which is invalid for 1-byte
  243. // branches.
  244. if ((Reloc->Size == 1) && (TargetDistance == 0))
  245. {
  246. OpcodeLocation = RelocLocation - 1;
  247. Opcode = Data + OpcodeLocation;
  248. // Check whether it belongs to a branch. If it does, it
  249. // is invalid.
  250. if (((Opcode [0] & M68K_Bcc_MASK_0) == M68K_Bcc_S_0) && (IsBinaryDataRange (Section, OpcodeLocation, OpcodeLocation + 2, Reloc)))
  251. {
  252. // If it is a BSR.S, removing it would modify the
  253. // semantics. Instead, insert a NOP at the end of the
  254. // section (if this is really the end of the section).
  255. if (Opcode [0] == M68K_BSR_S_0)
  256. {
  257. if (OpcodeLocation + 2 == Section->Size)
  258. {
  259. // Allocate two bytes at the end of the section.
  260. I1 *Space = AllocateSpaceInSection (Section, 2);
  261. if (Space)
  262. {
  263. Space [0] = M68K_NOP_0;
  264. Space [1] = M68K_NOP_1;
  265. }
  266. }
  267. }
  268. else
  269. {
  270. // Delete the reloc.
  271. FreeReloc (Reloc);
  272. // Cut or fill the gained space.
  273. M68kCutOrFillRange (Section, OpcodeLocation, OpcodeLocation + 2, OptimizeInfo);
  274. // Since the reloc has been removed, return from the function.
  275. return;
  276. }
  277. }
  278. }
  279. else if (OptimizeInfo->OptimizeBranches && (Reloc->Size == 2))
  280. {
  281. OpcodeLocation = RelocLocation - 2;
  282. Opcode = Data + OpcodeLocation;
  283. // Check whether the reloc belongs to a branch.
  284. if (((Opcode [0] & M68K_Bcc_MASK_0) == M68K_Bcc_W_0) && (Opcode [1] == M68K_Bcc_W_1) && (IsBinaryDataRange (Section, OpcodeLocation, OpcodeLocation + 4, Reloc)))
  285. {
  286. // Check whether it can be removed.
  287. if ((TargetDistance == 2) && (Opcode [0] != M68K_BSR_W_0))
  288. {
  289. // Delete the reloc.
  290. FreeReloc (Reloc);
  291. // Cut or fill the gained space.
  292. M68kCutOrFillRange (Section, OpcodeLocation, OpcodeLocation + 4, OptimizeInfo);
  293. // Since the reloc has been removed, return from the function.
  294. return;
  295. }
  296. // Check whether it is near enough for a Bcc.S.
  297. else if ((TargetDistance != 2) && (M68K_REL_OK (TargetDistance, 1)))
  298. {
  299. // Optimize it into a Bcc.S.
  300. Opcode [0] = M68K_Bcc_S_0 | (Data [OpcodeLocation] & (~M68K_Bcc_MASK_0));
  301. Opcode [1] = 0;
  302. // Change the reloc to 1-byte relative.
  303. Reloc->Size = 1;
  304. // Adjust the location.
  305. Reloc->Location--;
  306. RelocLocation--;
  307. // Adjust the offset. A short branch always
  308. // uses the next instruction as a reference.
  309. Reloc->FixedOffset--;
  310. // Cut or fill the gained space.
  311. M68kCutOrFillRange (Section, OpcodeLocation + 2, OpcodeLocation + 4, OptimizeInfo);
  312. }
  313. }
  314. }
  315. }
  316. // Other than that, only change 4-byte absolute relocs.
  317. else if (Reloc->Size == 4)
  318. {
  319. // Most opcodes are two bytes long, and the reloc follows
  320. // immediately.
  321. OpcodeLocation = RelocLocation - 2;
  322. // Safety check before accessing the section data.
  323. if (IsBinaryDataRange (Section, OpcodeLocation, OpcodeLocation + 6, Reloc))
  324. {
  325. Opcode = Data + OpcodeLocation;
  326. {
  327. // *** Branch Optimization ***
  328. // Try to optimize a 4-byte absolute branch into a 2-byte or 1-byte
  329. // relative one.
  330. // Check whether the reloc belongs to a branch.
  331. BOOLEAN IsJMP = ((Opcode [0] == M68K_JMP_0) && (Opcode [1] == M68K_JMP_1));
  332. BOOLEAN IsJSR = ((Opcode [0] == M68K_JSR_0) && (Opcode [1] == M68K_JSR_1));
  333. if ((IsJMP || IsJSR))
  334. {
  335. // Check whether it can be removed.
  336. if (OptimizeInfo->OptimizeBranches && (TargetDistance == 4) && IsJMP)
  337. {
  338. // Delete the reloc.
  339. FreeReloc (Reloc);
  340. // Cut or fill the gained space.
  341. M68kCutOrFillRange (Section, OpcodeLocation, OpcodeLocation + 6, OptimizeInfo);
  342. OptimizeInfo->OptimizeBranchesResult++;
  343. }
  344. // Check whether it is near enough for a BRA.S or BSR.S.
  345. else if (OptimizeInfo->OptimizeBranches && (TargetDistance != 4) && (M68K_REL_OK (TargetDistance, 1)))
  346. {
  347. // Optimize it into a BRA.S or BSR.S.
  348. Opcode [0] = (IsJSR ? M68K_BSR_S_0 : M68K_BRA_S_0);
  349. Opcode [1] = 0;
  350. // Change the reloc to 1-byte relative.
  351. Reloc->Relative = TRUE;
  352. Reloc->Size = 1;
  353. // Adjust the location.
  354. Reloc->Location--;
  355. // Adjust the offset. A short branch always
  356. // uses the next instruction as a reference.
  357. Reloc->FixedOffset--;
  358. // Cut or fill the gained space.
  359. M68kCutOrFillRange (Section, OpcodeLocation + 2, OpcodeLocation + 6, OptimizeInfo);
  360. OptimizeInfo->OptimizeBranchesResult++;
  361. }
  362. // Treat it as a normal absolute to relative optimization.
  363. else
  364. {
  365. BOOLEAN BranchOptimized = FALSE;
  366. // Check if the target is near enough for a BRA/BSR.
  367. if (M68K_REL_OK (TargetDistance, 2))
  368. {
  369. OptimizeInfo->OptimizeBranchesResult++;
  370. if (OptimizeInfo->NearAssemblyResult >= 0)
  371. OptimizeInfo->NearAssemblyResult += 2;
  372. if (OptimizeInfo->OptimizeBranches)
  373. {
  374. // Optimize it into a BRA.W or BSR.W.
  375. Opcode [0] = (IsJSR ? M68K_BSR_W_0 : M68K_BRA_W_0);
  376. Opcode [1] = (IsJSR ? M68K_BSR_W_1 : M68K_BRA_W_1);
  377. // Change the reloc to 2-byte relative.
  378. Reloc->Relative = TRUE;
  379. Reloc->Size = 2;
  380. // Cut or fill the gained space.
  381. M68kCutOrFillRange (Section, OpcodeLocation + 4, OpcodeLocation + 6, OptimizeInfo);
  382. // Do not try to do anything else with this branch.
  383. BranchOptimized = TRUE;
  384. }
  385. }
  386. else
  387. {
  388. // The target is not near enough. This means that near
  389. // assembly is not possible.
  390. OptimizeInfo->NearAssemblyResult = -1;
  391. }
  392. if (!BranchOptimized)
  393. {
  394. // Optimize F-Line jumps if desired.
  395. OptimizeInfo->UseFLineJumpsResult++;
  396. if (OptimizeInfo->Use4ByteFLineJumps)
  397. M68kEmuMakeFLineJump (Reloc, Opcode, IsJSR);
  398. else if (OptimizeInfo->UseFLineJumps)
  399. M68kTIOSMakeFLineJump (Reloc, Opcode, IsJSR);
  400. else
  401. return;
  402. // Cut or fill the gained space.
  403. M68kCutOrFillRange (Section, Reloc->Location + Reloc->Size, OpcodeLocation + 6, OptimizeInfo);
  404. }
  405. }
  406. }
  407. // Not a branch.
  408. else
  409. {
  410. BOOLEAN Optimized = FALSE;
  411. // Check if the target is near enough.
  412. if (M68K_REL_OK (TargetDistance, 2))
  413. {
  414. // *** Move Optimization ***
  415. // Optimize LEA(.L) var.L,reg into
  416. // LEA(.L) var.W(%PC),reg.
  417. if ((((Opcode [0] & M68K_LEA_ABS_MASK_0) == M68K_LEA_ABS_0) && ((Opcode [1] & M68K_LEA_ABS_MASK_1) == M68K_LEA_ABS_1))
  418. // Optimize PEA(.L) var.L into PEA(.L) var.W(%PC).
  419. || ((Opcode [0] == M68K_PEA_ABS_0) && (Opcode [1] == M68K_PEA_ABS_1))
  420. // Optimize MOVE.x var.L,reg/(reg)/(reg)+ into
  421. // MOVE.x var.W(%PC),reg/(reg)/(reg)+.
  422. || (((Opcode [0] & M68K_MOVE_ABS_REG_MASK_0) == M68K_MOVE_ABS_REG_0) && ((Opcode [1] & M68K_MOVE_ABS_REG_MASK_1) == M68K_MOVE_ABS_REG_1)
  423. && (!((Opcode [0] & M68K_MOVE_ABS_REG_INV_0_MASK_0) == M68K_MOVE_ABS_REG_INV_0_0))
  424. && (!(((Opcode [0] & M68K_MOVE_ABS_REG_INV_1_MASK_0) == M68K_MOVE_ABS_REG_INV_1_0) && ((Opcode [1] & M68K_MOVE_ABS_REG_INV_1_MASK_1) == M68K_MOVE_ABS_REG_INV_1_1))))
  425. // Optimize MOVE.x var.L,-(reg) into
  426. // MOVE.x var.W(%PC),-(reg).
  427. || (((Opcode [0] & M68K_MOVE_ABS_PREDEC_MASK_0) == M68K_MOVE_ABS_PREDEC_0) && ((Opcode [1] & M68K_MOVE_ABS_PREDEC_MASK_1) == M68K_MOVE_ABS_PREDEC_1)
  428. && (!((Opcode [0] & M68K_MOVE_ABS_PREDEC_INV_0_MASK_0) == M68K_MOVE_ABS_PREDEC_INV_0_0))))
  429. {
  430. OptimizeInfo->OptimizeMovesResult++;
  431. if (OptimizeInfo->NearAssemblyResult >= 0)
  432. OptimizeInfo->NearAssemblyResult += 2;
  433. if (OptimizeInfo->OptimizeMoves)
  434. {
  435. // Turn the opcode into a pc-relative one.
  436. M68K_MAKE_REL_OPCODE_1 (Opcode [1]);
  437. // Do everything else later.
  438. Optimized = TRUE;
  439. }
  440. }
  441. // *** Test Optimization ***
  442. // Optimize CMP.x var.L,reg into CMP.x var.W(%PC),reg.
  443. else if ((((Opcode [0] & M68K_CMP_ABS_REG_MASK_0) == M68K_CMP_ABS_REG_0) && ((Opcode [1] & M68K_CMP_ABS_REG_MASK_1) == M68K_CMP_ABS_REG_1)
  444. && (!((Opcode [1] & M68K_CMP_ABS_REG_INV_0_MASK_1) == M68K_CMP_ABS_REG_INV_0_1)))
  445. // Optimize BTST reg,var.L into BTST reg,var.W(%PC).
  446. || (((Opcode [0] & M68K_BTST_REG_ABS_MASK_0) == M68K_BTST_REG_ABS_0) && ((Opcode [1] & M68K_BTST_REG_ABS_MASK_1) == M68K_BTST_REG_ABS_1)))
  447. {
  448. OptimizeInfo->OptimizeTestsResult++;
  449. if (OptimizeInfo->NearAssemblyResult >= 0)
  450. OptimizeInfo->NearAssemblyResult += 2;
  451. if (OptimizeInfo->OptimizeTests)
  452. {
  453. // Turn the opcode into a pc-relative one.
  454. M68K_MAKE_REL_OPCODE_1 (Opcode [1]);
  455. // Do everything else later.
  456. Optimized = TRUE;
  457. }
  458. }
  459. // *** Calculation Optimization ***
  460. // Optimize ADD/SUB.x var.L,reg into
  461. // ADD/SUB.x var.W(%PC),reg.
  462. else if ((((Opcode [0] & M68K_ADDSUB_ABS_REG_0_MASK_0) == M68K_ADDSUB_ABS_REG_0_0) && ((Opcode [1] & M68K_ADDSUB_ABS_REG_0_MASK_1) == M68K_ADDSUB_ABS_REG_0_1))
  463. || (((Opcode [0] & M68K_ADDSUB_ABS_REG_1_MASK_0) == M68K_ADDSUB_ABS_REG_1_0) && ((Opcode [1] & M68K_ADDSUB_ABS_REG_1_MASK_1) == M68K_ADDSUB_ABS_REG_1_1))
  464. // Optimize MUL/DIV.x var.L,reg into
  465. // MUL/DIV.x var.W(%PC),reg.
  466. || (((Opcode [0] & M68K_MULDIV_ABS_REG_MASK_0) == M68K_MULDIV_ABS_REG_0) && ((Opcode [1] & M68K_MULDIV_ABS_REG_MASK_1) == M68K_MULDIV_ABS_REG_1))
  467. // Optimize AND/OR.x var.L,reg into
  468. // AND/OR.x var.W(%PC),reg.
  469. || (((Opcode [0] & M68K_ANDOR_ABS_REG_MASK_0) == M68K_ANDOR_ABS_REG_0) && ((Opcode [1] & M68K_ANDOR_ABS_REG_MASK_1) == M68K_ANDOR_ABS_REG_1)))
  470. {
  471. OptimizeInfo->OptimizeCalcsResult++;
  472. if (OptimizeInfo->NearAssemblyResult >= 0)
  473. OptimizeInfo->NearAssemblyResult += 2;
  474. if (OptimizeInfo->OptimizeCalcs)
  475. {
  476. // Turn the opcode into a pc-relative one.
  477. M68K_MAKE_REL_OPCODE_1 (Opcode [1]);
  478. // Do everything else later.
  479. Optimized = TRUE;
  480. }
  481. }
  482. }
  483. if (Optimized)
  484. {
  485. // Change the reloc to 2-byte relative.
  486. Reloc->Relative = TRUE;
  487. Reloc->Size = 2;
  488. // Cut or fill the gained space.
  489. M68kCutOrFillRange (Section, OpcodeLocation + 4, OpcodeLocation + 6, OptimizeInfo);
  490. }
  491. else
  492. {
  493. // Check if the target is near enough.
  494. if (M68K_REL_OK (TargetDistance, 2))
  495. {
  496. if (IsBinaryDataRange (Section, OpcodeLocation, OpcodeLocation + 8, Reloc))
  497. {
  498. // Optimize MOVE.x var.L,ofs(reg) into
  499. // MOVE.x var.W(%PC),ofs(reg)
  500. // We cannot handle this above because of the
  501. // offset, which comes after the reloc.
  502. if (((Opcode [0] & M68K_MOVE_ABS_OFSREG_MASK_0) == M68K_MOVE_ABS_OFSREG_0) && ((Opcode [1] & M68K_MOVE_ABS_OFSREG_MASK_1) == M68K_MOVE_ABS_OFSREG_1)
  503. && (!((Opcode [0] & M68K_MOVE_ABS_OFSREG_INV_0_MASK_0) == M68K_MOVE_ABS_OFSREG_INV_0_0)))
  504. {
  505. OptimizeInfo->OptimizeMovesResult++;
  506. if (OptimizeInfo->NearAssemblyResult >= 0)
  507. OptimizeInfo->NearAssemblyResult += 2;
  508. if (OptimizeInfo->OptimizeMoves)
  509. {
  510. // Turn the opcode into a pc-relative one.
  511. M68K_MAKE_REL_OPCODE_1 (Opcode [1]);
  512. // Move the offset to the correct place.
  513. Opcode [4] = Opcode [6];
  514. Opcode [5] = Opcode [7];
  515. // Change the reloc to 2-byte relative.
  516. Reloc->Relative = TRUE;
  517. Reloc->Size = 2;
  518. // Cut or fill the gained space.
  519. M68kCutOrFillRange (Section, OpcodeLocation + 6, OpcodeLocation + 8, OptimizeInfo);
  520. }
  521. }
  522. }
  523. {
  524. OpcodeLocation = RelocLocation - 4;
  525. Opcode = Data + OpcodeLocation;
  526. if (IsBinaryDataRange (Section, OpcodeLocation, OpcodeLocation + 8, Reloc))
  527. {
  528. // Optimize MOVEM.x var.L,regs into
  529. // MOVEM var.W(%PC),regs.
  530. if (((Opcode [0] & M68K_MOVEM_ABS_REGS_MASK_0) == M68K_MOVEM_ABS_REGS_0) && ((Opcode [1] & M68K_MOVEM_ABS_REGS_MASK_1) == M68K_MOVEM_ABS_REGS_1))
  531. {
  532. OptimizeInfo->OptimizeMovesResult++;
  533. if (OptimizeInfo->NearAssemblyResult >= 0)
  534. OptimizeInfo->NearAssemblyResult += 2;
  535. if (OptimizeInfo->OptimizeMoves)
  536. {
  537. // Turn the opcode into a pc-relative one.
  538. M68K_MAKE_REL_OPCODE_1 (Opcode [1]);
  539. // Change the reloc to 2-byte relative.
  540. Reloc->Relative = TRUE;
  541. Reloc->Size = 2;
  542. // Cut or fill the gained space.
  543. M68kCutOrFillRange (Section, OpcodeLocation + 6, OpcodeLocation + 8, OptimizeInfo);
  544. }
  545. }
  546. // Optimize BTST #num,var.L into
  547. // BTST #num,var.W(%PC).
  548. else if ((Opcode [0] == M68K_BTST_IMM_ABS_0) && (Opcode [1] == M68K_BTST_IMM_ABS_1) && (Opcode [2] == M68K_BTST_IMM_ABS_2))
  549. {
  550. OptimizeInfo->OptimizeTestsResult++;
  551. if (OptimizeInfo->NearAssemblyResult >= 0)
  552. OptimizeInfo->NearAssemblyResult += 2;
  553. if (OptimizeInfo->OptimizeTests)
  554. {
  555. // Turn the opcode into a pc-relative one.
  556. M68K_MAKE_REL_OPCODE_1 (Opcode [1]);
  557. // Change the reloc to 2-byte relative.
  558. Reloc->Relative = TRUE;
  559. Reloc->Size = 2;
  560. // Cut or fill the gained space.
  561. M68kCutOrFillRange (Section, OpcodeLocation + 6, OpcodeLocation + 8, OptimizeInfo);
  562. }
  563. }
  564. }
  565. }
  566. }
  567. else
  568. {
  569. // The target is not near enough. This means that near
  570. // assembly is not possible.
  571. OptimizeInfo->NearAssemblyResult = -1;
  572. }
  573. }
  574. }
  575. }
  576. }
  577. }
  578. }
  579. // Checks if a specific reloc might be optimizable. This is currently
  580. // limited to 4-bytes absolute relocs because that is the only case
  581. // this is needed for.
  582. BOOLEAN M68kIsRelocOptimizable (const RELOC *Reloc)
  583. {
  584. if (Reloc->Unoptimizable) return FALSE;
  585. if (Reloc->Size == 4 && (!(Reloc->Relative)))
  586. {
  587. const SECTION *Section = Reloc->Parent;
  588. // Most opcodes are two bytes long, and the reloc follows
  589. // immediately.
  590. OFFSET OpcodeLocation = Reloc->Location - 2;
  591. // Safety check before accessing the section data.
  592. if (IsBinaryDataRange (Section, OpcodeLocation, OpcodeLocation + 6, Reloc))
  593. {
  594. const I1 *Opcode = Section->Data + OpcodeLocation;
  595. // *** Branch Optimization ***
  596. if (((Opcode [0] == M68K_JMP_0) && (Opcode [1] == M68K_JMP_1))
  597. || ((Opcode [0] == M68K_JSR_0) && (Opcode [1] == M68K_JSR_1))
  598. // *** Move Optimization ***
  599. || (((Opcode [0] & M68K_LEA_ABS_MASK_0) == M68K_LEA_ABS_0) && ((Opcode [1] & M68K_LEA_ABS_MASK_1) == M68K_LEA_ABS_1))
  600. || ((Opcode [0] == M68K_PEA_ABS_0) && (Opcode [1] == M68K_PEA_ABS_1))
  601. || (((Opcode [0] & M68K_MOVE_ABS_REG_MASK_0) == M68K_MOVE_ABS_REG_0) && ((Opcode [1] & M68K_MOVE_ABS_REG_MASK_1) == M68K_MOVE_ABS_REG_1)
  602. && (!((Opcode [0] & M68K_MOVE_ABS_REG_INV_0_MASK_0) == M68K_MOVE_ABS_REG_INV_0_0))
  603. && (!(((Opcode [0] & M68K_MOVE_ABS_REG_INV_1_MASK_0) == M68K_MOVE_ABS_REG_INV_1_0) && ((Opcode [1] & M68K_MOVE_ABS_REG_INV_1_MASK_1) == M68K_MOVE_ABS_REG_INV_1_1))))
  604. || (((Opcode [0] & M68K_MOVE_ABS_PREDEC_MASK_0) == M68K_MOVE_ABS_PREDEC_0) && ((Opcode [1] & M68K_MOVE_ABS_PREDEC_MASK_1) == M68K_MOVE_ABS_PREDEC_1)
  605. && (!((Opcode [0] & M68K_MOVE_ABS_PREDEC_INV_0_MASK_0) == M68K_MOVE_ABS_PREDEC_INV_0_0)))
  606. || (((Opcode [0] & M68K_MOVE_ABS_OFSREG_MASK_0) == M68K_MOVE_ABS_OFSREG_0) && ((Opcode [1] & M68K_MOVE_ABS_OFSREG_MASK_1) == M68K_MOVE_ABS_OFSREG_1)
  607. && (!((Opcode [0] & M68K_MOVE_ABS_OFSREG_INV_0_MASK_0) == M68K_MOVE_ABS_OFSREG_INV_0_0)))
  608. || (((Opcode [0] & M68K_MOVEM_ABS_REGS_MASK_0) == M68K_MOVEM_ABS_REGS_0) && ((Opcode [1] & M68K_MOVEM_ABS_REGS_MASK_1) == M68K_MOVEM_ABS_REGS_1))
  609. // *** Test Optimization ***
  610. || (((Opcode [0] & M68K_CMP_ABS_REG_MASK_0) == M68K_CMP_ABS_REG_0) && ((Opcode [1] & M68K_CMP_ABS_REG_MASK_1) == M68K_CMP_ABS_REG_1)
  611. && (!((Opcode [1] & M68K_CMP_ABS_REG_INV_0_MASK_1) == M68K_CMP_ABS_REG_INV_0_1)))
  612. || (((Opcode [0] & M68K_BTST_REG_ABS_MASK_0) == M68K_BTST_REG_ABS_0) && ((Opcode [1] & M68K_BTST_REG_ABS_MASK_1) == M68K_BTST_REG_ABS_1))
  613. || ((Opcode [0] == M68K_BTST_IMM_ABS_0) && (Opcode [1] == M68K_BTST_IMM_ABS_1) && (Opcode [2] == M68K_BTST_IMM_ABS_2))
  614. // *** Calculation Optimization ***
  615. || (((Opcode [0] & M68K_ADDSUB_ABS_REG_0_MASK_0) == M68K_ADDSUB_ABS_REG_0_0) && ((Opcode [1] & M68K_ADDSUB_ABS_REG_0_MASK_1) == M68K_ADDSUB_ABS_REG_0_1))
  616. || (((Opcode [0] & M68K_ADDSUB_ABS_REG_1_MASK_0) == M68K_ADDSUB_ABS_REG_1_0) && ((Opcode [1] & M68K_ADDSUB_ABS_REG_1_MASK_1) == M68K_ADDSUB_ABS_REG_1_1))
  617. || (((Opcode [0] & M68K_MULDIV_ABS_REG_MASK_0) == M68K_MULDIV_ABS_REG_0) && ((Opcode [1] & M68K_MULDIV_ABS_REG_MASK_1) == M68K_MULDIV_ABS_REG_1))
  618. || (((Opcode [0] & M68K_ANDOR_ABS_REG_MASK_0) == M68K_ANDOR_ABS_REG_0) && ((Opcode [1] & M68K_ANDOR_ABS_REG_MASK_1) == M68K_ANDOR_ABS_REG_1)))
  619. return TRUE;
  620. }
  621. }
  622. return FALSE;
  623. }
  624. // If the section ends with exactly one NOP instruction, remove the NOP.
  625. void M68kRemoveTrailingNOP (SECTION *Section)
  626. {
  627. I1 *Data = Section->Data;
  628. SIZE Size = Section->Size;
  629. // Validate basic circumstances.
  630. if (Data && (Size >= 4)
  631. // Check for NOP in last instruction.
  632. && (Data [Size - 2] == M68K_NOP_0) && (Data [Size - 1] == M68K_NOP_1) && (IsBinaryDataRange (Section, Size - 2, Size, NULL))
  633. // Check for NOP in previous instruction (which would mean that we
  634. // actually want the NOPs to be there).
  635. && (!((Data [Size - 4] == M68K_NOP_0) && (Data [Size - 3] == M68K_NOP_1)))
  636. // Check if something prevents us from shrinking the section.
  637. && (CanShrinkSection (Section, Size - 2, NULL)))
  638. // Cut the NOP away.
  639. CutSection (Section, Size - 2);
  640. }
  641. // Fix and return the target offset of a reloc.
  642. // In particular, for 1-byte relative relocs, the target offset is
  643. // increased by 1, so the the reloc points to the symbol and not to
  644. // the symbol minus 1.
  645. OFFSET M68kFixTargetOffset (OFFSET Offset, SIZE RelocSize, BOOLEAN RelocRelative)
  646. {
  647. if (RelocRelative && (RelocSize == 1))
  648. return Offset + 1;
  649. else
  650. return Offset;
  651. }
  652. #define SHORT_IMPORTANCE 2048
  653. // Called by M68kGetSectionRelationship; see below.
  654. COUNT M68kGetRelocImportance (const RELOC *Reloc, OFFSET Offset)
  655. {
  656. SECTION *Section = Reloc->Parent;
  657. if (Reloc->Unoptimizable) return 0;
  658. // Byte offsets are useful only for jumps or branches, so detect
  659. // them.
  660. if (M68K_REL_OK (Offset, 1))
  661. {
  662. OFFSET RelocLocation = Reloc->Location;
  663. OFFSET OpcodeLocation = RelocLocation - 2;
  664. I1 *Opcode = Section->Data + OpcodeLocation;
  665. if (Reloc->Size == 4)
  666. {
  667. // Check whether the reloc belongs to a branch.
  668. BOOLEAN IsJMP = ((Opcode [0] == M68K_JMP_0)
  669. && (Opcode [1] == M68K_JMP_1));
  670. BOOLEAN IsJSR = ((Opcode [0] == M68K_JSR_0)
  671. && (Opcode [1] == M68K_JSR_1));
  672. if ((IsJMP || IsJSR)
  673. && IsBinaryDataRange (Section,
  674. OpcodeLocation,
  675. OpcodeLocation + 6, Reloc))
  676. {
  677. return ((Offset == 4 && IsJMP) ? SHORT_IMPORTANCE * 2
  678. : SHORT_IMPORTANCE);
  679. }
  680. }
  681. else if (Reloc->Size == 2)
  682. {
  683. // Check whether the reloc belongs to a branch.
  684. if ((Opcode [0] & M68K_Bcc_MASK_0) == M68K_Bcc_W_0
  685. && Opcode [1] == M68K_Bcc_W_1
  686. && IsBinaryDataRange (Section,
  687. OpcodeLocation,
  688. OpcodeLocation + 4, Reloc))
  689. {
  690. return (Offset == 2 && !(Opcode [0] == M68K_BSR_W_0
  691. && Opcode [1] == M68K_BSR_W_1)) ? SHORT_IMPORTANCE * 2
  692. : SHORT_IMPORTANCE;
  693. }
  694. }
  695. }
  696. // Everything else is not optimizable.
  697. return 0;
  698. }
  699. // Determines the amount of relationship between the two sections, for the
  700. // situation that Section2 might be put just behind Section1. A reference
  701. // that could potentially be short gets 2048 points; a reference that could
  702. // potentially be removed gets twice as much.
  703. // The effect is that a section with one potentially short reference can
  704. // be at most 2048 bytes long for being inserted immediately by reordering,
  705. // and so on.
  706. COUNT M68kGetSectionRelationship (const SECTION *Section1, const SECTION *Section2)
  707. {
  708. COUNT Result = 0;
  709. const RELOC *Reloc;
  710. for_each (Reloc, Section1->Relocs)
  711. {
  712. const SYMBOL *TargetSymbol = Reloc->Target.Symbol;
  713. if (TargetSymbol && TargetSymbol->Parent == Section2)
  714. Result += M68kGetRelocImportance (Reloc, Section1->Size - Reloc->Location + TargetSymbol->Location + Reloc->Target.Offset + Reloc->FixedOffset);
  715. }
  716. for_each (Reloc, Section2->Relocs)
  717. {
  718. const SYMBOL *TargetSymbol = Reloc->Target.Symbol;
  719. if (TargetSymbol && TargetSymbol->Parent == Section1)
  720. Result += M68kGetRelocImportance (Reloc, TargetSymbol->Location + Reloc->Target.Offset + Reloc->FixedOffset - Section1->Size - Reloc->Location);
  721. }
  722. return Result;
  723. }