fix_m68k.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935
  1. /* fix_m68k.c: Routines for M68000 code fixup
  2. Copyright (C) 2003 Sebastian Reichelt
  3. Copyright (C) 2003-2007 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. // Returns the number of bytes removed.
  82. static COUNT M68kCutOrFillRange (SECTION *Section, OFFSET Start, OFFSET End, OPTIMIZE_INFO *OptimizeInfo)
  83. {
  84. if (End > Start)
  85. {
  86. BOOLEAN SectionEnd = End >= Section->Size;
  87. // Make sure Start is not outside of the section data;
  88. if (Start < 0)
  89. Start = 0;
  90. // Make sure End is not outside of the section data;
  91. if (SectionEnd)
  92. End = Section->Size;
  93. // If the range is at the end of the section, shrink the section.
  94. if (SectionEnd && CanShrinkSection (Section, Start, NULL))
  95. {
  96. // Look for a short branch to the end of the section. GNU as
  97. // does this to simulate long conditional branches.
  98. BOOLEAN BranchFound = FALSE;
  99. const I1 *Data = Section->Data;
  100. if (Data)
  101. {
  102. SIZE Size = Section->Size;
  103. OFFSET CurPos;
  104. for (CurPos = Size - 4; (CurPos >= 0) && (CurPos >= Size - 8); CurPos -= 2)
  105. {
  106. if (((Data [CurPos + 0] & M68K_Bcc_MASK_0) == M68K_Bcc_S_0) && (Data [CurPos + 1] == Size - (CurPos + 2)))
  107. {
  108. BranchFound = TRUE;
  109. break;
  110. }
  111. }
  112. }
  113. if (!BranchFound)
  114. {
  115. // No such branch was found, so we can (hopefully) safely cut
  116. // the section.
  117. CutSection (Section, Start);
  118. return End - Start;
  119. }
  120. }
  121. // If the range is in the middle of the section, try to cut it out.
  122. else
  123. {
  124. if (CanCutRange (Section, Start, End))
  125. {
  126. SIZE Length = End - Start;
  127. OptimizeInfo->CutRangesResult += Length;
  128. if (OptimizeInfo->CutRanges)
  129. {
  130. CutRange (Section, Start, End);
  131. OptimizeInfo->NearAssemblyResult -= Length;
  132. return Length;
  133. }
  134. }
  135. }
  136. // If we cannot cut the range for some reason, fill it with NOPs.
  137. {
  138. I1 *Data = Section->Data;
  139. if (Data)
  140. {
  141. OFFSET CurPos;
  142. for (CurPos = Start; CurPos + 1 < End; CurPos += 2)
  143. {
  144. Data [CurPos + 0] = M68K_NOP_0;
  145. Data [CurPos + 1] = M68K_NOP_1;
  146. }
  147. }
  148. return 0;
  149. }
  150. }
  151. else
  152. return 0;
  153. }
  154. // Fix and possibly optimize a given relocation entry.
  155. // TargetDistance is the estimated (signed) distance of the relocation target, including the
  156. // fixed offset. OptimizeInfo contains information about what to optimize.
  157. // The reloc may be removed during the process.
  158. void M68kFixReloc (RELOC *Reloc, OFFSET TargetDistance, OPTIMIZE_INFO *OptimizeInfo)
  159. {
  160. SECTION *Section = Reloc->Parent;
  161. I1 *Data = Section->Data;
  162. OFFSET RelocLocation = Reloc->Location;
  163. OFFSET OpcodeLocation;
  164. I1 *Opcode;
  165. if (!Data)
  166. return;
  167. if (Reloc->Unoptimizable)
  168. return;
  169. // Be careful with relocs at the beginning of the section; they might
  170. // cause segmentation faults when we try to determine the opcode.
  171. // Usually, ignore all relocs whose location is less than 2.
  172. // As a special exception, allow 1-byte relocs with a location of 1.
  173. if (!((Reloc->Location >= 2) || ((Reloc->Size == 1) && (Reloc->Location == 1))))
  174. return;
  175. // Optimize a subroutine branch followed by an RTS.
  176. if (OptimizeInfo->OptimizeReturns)
  177. {
  178. OFFSET RelocEnd = RelocLocation + Reloc->Size;
  179. OFFSET RTSEnd = RelocEnd + 2;
  180. if ((RTSEnd <= Section->Size) && (Data [RelocEnd + 0] == M68K_RTS_0) && (Data [RelocEnd + 1] == M68K_RTS_1))
  181. {
  182. OFFSET OpcodeLocation;
  183. I1 *Opcode;
  184. BOOLEAN Optimized = FALSE;
  185. if (Reloc->Relative)
  186. {
  187. switch (Reloc->Size)
  188. {
  189. // Optimize 1-byte relative relocs.
  190. case 1:
  191. OpcodeLocation = RelocLocation - 1;
  192. Opcode = Data + OpcodeLocation;
  193. // Check if the reloc belongs to a BSR.S, and that there
  194. // is nothing in our way.
  195. if ((Opcode [0] == M68K_BSR_S_0) && (IsBinaryDataRange (Section, OpcodeLocation, RTSEnd, Reloc)))
  196. {
  197. // Optimize it into a BRA.S.
  198. Opcode [0] = M68K_BRA_S_0;
  199. Optimized = TRUE;
  200. }
  201. break;
  202. // Optimize 2-byte relative relocs.
  203. case 2:
  204. OpcodeLocation = RelocLocation - 2;
  205. Opcode = Data + OpcodeLocation;
  206. // Check if the reloc belongs to a BSR.W, and that there
  207. // is nothing in our way.
  208. if ((Opcode [0] == M68K_BSR_W_0) && (Opcode [1] == M68K_BSR_W_1) && (IsBinaryDataRange (Section, OpcodeLocation, RTSEnd, Reloc)))
  209. {
  210. // Optimize it into a JMP.
  211. Opcode [0] = M68K_BRA_W_0;
  212. Opcode [1] = M68K_BRA_W_1;
  213. Optimized = TRUE;
  214. }
  215. break;
  216. }
  217. }
  218. else
  219. {
  220. // Optimize 4-byte absolute relocs.
  221. if (Reloc->Size == 4)
  222. {
  223. OpcodeLocation = RelocLocation - 2;
  224. Opcode = Data + OpcodeLocation;
  225. // Check if the reloc belongs to a JSR, and that there is
  226. // nothing in our way.
  227. if ((Opcode [0] == M68K_JSR_0) && (Opcode [1] == M68K_JSR_1) && (IsBinaryDataRange (Section, OpcodeLocation, RTSEnd, Reloc)))
  228. {
  229. // Optimize it into a JMP.
  230. Opcode [0] = M68K_JMP_0;
  231. Opcode [1] = M68K_JMP_1;
  232. Optimized = TRUE;
  233. }
  234. }
  235. }
  236. // Remove the RTS.
  237. if (Optimized)
  238. TargetDistance -= M68kCutOrFillRange (Section, RelocEnd, RTSEnd, OptimizeInfo);
  239. }
  240. }
  241. // Only branch fixes and optimizations make sense for relative relocs.
  242. if (Reloc->Relative)
  243. {
  244. // Check if it is a 1-byte relative reloc at the end of the
  245. // section, pointing to the next instruction. This means that
  246. // the value will probably be 0, which is invalid for 1-byte
  247. // branches.
  248. if ((Reloc->Size == 1) && (TargetDistance == 0))
  249. {
  250. OpcodeLocation = RelocLocation - 1;
  251. Opcode = Data + OpcodeLocation;
  252. // Check whether it belongs to a branch. If it does, it
  253. // is invalid.
  254. if (((Opcode [0] & M68K_Bcc_MASK_0) == M68K_Bcc_S_0) && (IsBinaryDataRange (Section, OpcodeLocation, OpcodeLocation + 2, Reloc)))
  255. {
  256. // If it is a BSR.S, removing it would modify the
  257. // semantics. Instead, insert a NOP at the end of the
  258. // section (if this is really the end of the section).
  259. if (Opcode [0] == M68K_BSR_S_0)
  260. {
  261. if (OpcodeLocation + 2 == Section->Size)
  262. {
  263. // Allocate two bytes at the end of the section.
  264. I1 *Space = AllocateSpaceInSection (Section, 2);
  265. if (Space)
  266. {
  267. Space [0] = M68K_NOP_0;
  268. Space [1] = M68K_NOP_1;
  269. }
  270. }
  271. }
  272. else
  273. {
  274. // Delete the reloc.
  275. FreeReloc (Reloc);
  276. // Cut or fill the gained space.
  277. M68kCutOrFillRange (Section, OpcodeLocation, OpcodeLocation + 2, OptimizeInfo);
  278. // Since the reloc has been removed, return from the function.
  279. return;
  280. }
  281. }
  282. }
  283. else if (OptimizeInfo->OptimizeBranches && (Reloc->Size == 2))
  284. {
  285. OpcodeLocation = RelocLocation - 2;
  286. Opcode = Data + OpcodeLocation;
  287. // Check whether the reloc belongs to a branch.
  288. if (((Opcode [0] & M68K_Bcc_MASK_0) == M68K_Bcc_W_0) && (Opcode [1] == M68K_Bcc_W_1) && (IsBinaryDataRange (Section, OpcodeLocation, OpcodeLocation + 4, Reloc)))
  289. {
  290. // Check whether it can be removed.
  291. if ((TargetDistance == 2) && (Opcode [0] != M68K_BSR_W_0))
  292. {
  293. // Delete the reloc.
  294. FreeReloc (Reloc);
  295. // Cut or fill the gained space.
  296. M68kCutOrFillRange (Section, OpcodeLocation, OpcodeLocation + 4, OptimizeInfo);
  297. // Since the reloc has been removed, return from the function.
  298. return;
  299. }
  300. // Check whether it is near enough for a Bcc.S.
  301. else if ((TargetDistance != 2) && (M68K_REL_OK (TargetDistance, 1)))
  302. {
  303. // Optimize it into a Bcc.S.
  304. Opcode [0] = M68K_Bcc_S_0 | (Data [OpcodeLocation] & (~M68K_Bcc_MASK_0));
  305. Opcode [1] = 0;
  306. // Change the reloc to 1-byte relative.
  307. Reloc->Size = 1;
  308. // Adjust the location.
  309. Reloc->Location--;
  310. RelocLocation--;
  311. // Adjust the offset. A short branch always
  312. // uses the next instruction as a reference.
  313. Reloc->FixedOffset--;
  314. // Cut or fill the gained space.
  315. M68kCutOrFillRange (Section, OpcodeLocation + 2, OpcodeLocation + 4, OptimizeInfo);
  316. }
  317. }
  318. }
  319. }
  320. // Other than that, only change 4-byte absolute relocs.
  321. else if (Reloc->Size == 4)
  322. {
  323. // Most opcodes are two bytes long, and the reloc follows
  324. // immediately.
  325. OpcodeLocation = RelocLocation - 2;
  326. // Safety check before accessing the section data.
  327. if (IsBinaryDataRange (Section, OpcodeLocation, OpcodeLocation + 6, Reloc))
  328. {
  329. Opcode = Data + OpcodeLocation;
  330. {
  331. // *** Branch Optimization ***
  332. // Try to optimize a 4-byte absolute branch into a 2-byte or 1-byte
  333. // relative one.
  334. // Check whether the reloc belongs to a branch.
  335. BOOLEAN IsJMP = ((Opcode [0] == M68K_JMP_0) && (Opcode [1] == M68K_JMP_1));
  336. BOOLEAN IsJSR = ((Opcode [0] == M68K_JSR_0) && (Opcode [1] == M68K_JSR_1));
  337. if ((IsJMP || IsJSR))
  338. {
  339. // Check whether it can be removed.
  340. if (OptimizeInfo->OptimizeBranches && (TargetDistance == 4) && IsJMP)
  341. {
  342. // Delete the reloc.
  343. FreeReloc (Reloc);
  344. // Cut or fill the gained space.
  345. M68kCutOrFillRange (Section, OpcodeLocation, OpcodeLocation + 6, OptimizeInfo);
  346. OptimizeInfo->OptimizeBranchesResult++;
  347. }
  348. // Check whether it is near enough for a BRA.S or BSR.S.
  349. else if (OptimizeInfo->OptimizeBranches && (TargetDistance != 4) && (M68K_REL_OK (TargetDistance, 1)))
  350. {
  351. // Optimize it into a BRA.S or BSR.S.
  352. Opcode [0] = (IsJSR ? M68K_BSR_S_0 : M68K_BRA_S_0);
  353. Opcode [1] = 0;
  354. // Change the reloc to 1-byte relative.
  355. Reloc->Relative = TRUE;
  356. Reloc->Size = 1;
  357. // Adjust the location.
  358. Reloc->Location--;
  359. // Adjust the offset. A short branch always
  360. // uses the next instruction as a reference.
  361. Reloc->FixedOffset--;
  362. // Cut or fill the gained space.
  363. M68kCutOrFillRange (Section, OpcodeLocation + 2, OpcodeLocation + 6, OptimizeInfo);
  364. OptimizeInfo->OptimizeBranchesResult++;
  365. }
  366. // Treat it as a normal absolute to relative optimization.
  367. else
  368. {
  369. BOOLEAN BranchOptimized = FALSE;
  370. // Check if the target is near enough for a BRA/BSR.
  371. if (M68K_REL_OK (TargetDistance, 2))
  372. {
  373. OptimizeInfo->OptimizeBranchesResult++;
  374. if (OptimizeInfo->NearAssemblyResult >= 0)
  375. OptimizeInfo->NearAssemblyResult += 2;
  376. if (OptimizeInfo->OptimizeBranches)
  377. {
  378. // Optimize it into a BRA.W or BSR.W.
  379. Opcode [0] = (IsJSR ? M68K_BSR_W_0 : M68K_BRA_W_0);
  380. Opcode [1] = (IsJSR ? M68K_BSR_W_1 : M68K_BRA_W_1);
  381. // Change the reloc to 2-byte relative.
  382. Reloc->Relative = TRUE;
  383. Reloc->Size = 2;
  384. // Cut or fill the gained space.
  385. M68kCutOrFillRange (Section, OpcodeLocation + 4, OpcodeLocation + 6, OptimizeInfo);
  386. // Do not try to do anything else with this branch.
  387. BranchOptimized = TRUE;
  388. }
  389. }
  390. else
  391. {
  392. // The target is not near enough. This means that near
  393. // assembly is not possible.
  394. OptimizeInfo->NearAssemblyResult = -1;
  395. }
  396. if (!BranchOptimized)
  397. {
  398. // Optimize F-Line jumps if desired.
  399. OptimizeInfo->UseFLineJumpsResult++;
  400. if (OptimizeInfo->Use4ByteFLineJumps)
  401. M68kEmuMakeFLineJump (Reloc, Opcode, IsJSR);
  402. else if (OptimizeInfo->UseFLineJumps)
  403. M68kTIOSMakeFLineJump (Reloc, Opcode, IsJSR);
  404. else
  405. return;
  406. // Cut or fill the gained space.
  407. M68kCutOrFillRange (Section, Reloc->Location + Reloc->Size, OpcodeLocation + 6, OptimizeInfo);
  408. }
  409. }
  410. }
  411. // Not a branch.
  412. else
  413. {
  414. BOOLEAN Optimized = FALSE;
  415. // Check if the target is near enough.
  416. if (M68K_REL_OK (TargetDistance, 2))
  417. {
  418. // *** Move Optimization ***
  419. // Optimize LEA(.L) var.L,reg into
  420. // LEA(.L) var.W(%PC),reg.
  421. if ((((Opcode [0] & M68K_LEA_ABS_MASK_0) == M68K_LEA_ABS_0) && ((Opcode [1] & M68K_LEA_ABS_MASK_1) == M68K_LEA_ABS_1))
  422. // Optimize PEA(.L) var.L into PEA(.L) var.W(%PC).
  423. || ((Opcode [0] == M68K_PEA_ABS_0) && (Opcode [1] == M68K_PEA_ABS_1))
  424. // Optimize MOVE.x var.L,reg/(reg)/(reg)+ into
  425. // MOVE.x var.W(%PC),reg/(reg)/(reg)+.
  426. || (((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)
  427. && (!((Opcode [0] & M68K_MOVE_ABS_REG_INV_0_MASK_0) == M68K_MOVE_ABS_REG_INV_0_0))
  428. && (!(((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))))
  429. // Optimize MOVE.x var.L,-(reg) into
  430. // MOVE.x var.W(%PC),-(reg).
  431. || (((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)
  432. && (!((Opcode [0] & M68K_MOVE_ABS_PREDEC_INV_0_MASK_0) == M68K_MOVE_ABS_PREDEC_INV_0_0))))
  433. {
  434. OptimizeInfo->OptimizeMovesResult++;
  435. if (OptimizeInfo->NearAssemblyResult >= 0)
  436. OptimizeInfo->NearAssemblyResult += 2;
  437. if (OptimizeInfo->OptimizeMoves)
  438. {
  439. // Turn the opcode into a pc-relative one.
  440. M68K_MAKE_REL_OPCODE_1 (Opcode [1]);
  441. // Do everything else later.
  442. Optimized = TRUE;
  443. }
  444. }
  445. // *** Test Optimization ***
  446. // Optimize CMP.x var.L,reg into CMP.x var.W(%PC),reg.
  447. 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)
  448. && (!((Opcode [1] & M68K_CMP_ABS_REG_INV_0_MASK_1) == M68K_CMP_ABS_REG_INV_0_1)))
  449. // Optimize BTST reg,var.L into BTST reg,var.W(%PC).
  450. || (((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)))
  451. {
  452. OptimizeInfo->OptimizeTestsResult++;
  453. if (OptimizeInfo->NearAssemblyResult >= 0)
  454. OptimizeInfo->NearAssemblyResult += 2;
  455. if (OptimizeInfo->OptimizeTests)
  456. {
  457. // Turn the opcode into a pc-relative one.
  458. M68K_MAKE_REL_OPCODE_1 (Opcode [1]);
  459. // Do everything else later.
  460. Optimized = TRUE;
  461. }
  462. }
  463. // *** Calculation Optimization ***
  464. // Optimize ADD/SUB.x var.L,reg into
  465. // ADD/SUB.x var.W(%PC),reg.
  466. 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))
  467. || (((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))
  468. // Optimize MUL/DIV.x var.L,reg into
  469. // MUL/DIV.x var.W(%PC),reg.
  470. || (((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))
  471. // Optimize AND/OR.x var.L,reg into
  472. // AND/OR.x var.W(%PC),reg.
  473. || (((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)))
  474. {
  475. OptimizeInfo->OptimizeCalcsResult++;
  476. if (OptimizeInfo->NearAssemblyResult >= 0)
  477. OptimizeInfo->NearAssemblyResult += 2;
  478. if (OptimizeInfo->OptimizeCalcs)
  479. {
  480. // Turn the opcode into a pc-relative one.
  481. M68K_MAKE_REL_OPCODE_1 (Opcode [1]);
  482. // Do everything else later.
  483. Optimized = TRUE;
  484. }
  485. }
  486. }
  487. if (Optimized)
  488. {
  489. // Change the reloc to 2-byte relative.
  490. Reloc->Relative = TRUE;
  491. Reloc->Size = 2;
  492. // Cut or fill the gained space.
  493. M68kCutOrFillRange (Section, OpcodeLocation + 4, OpcodeLocation + 6, OptimizeInfo);
  494. }
  495. else
  496. {
  497. // Check if the target is near enough.
  498. if (M68K_REL_OK (TargetDistance, 2))
  499. {
  500. if (IsBinaryDataRange (Section, OpcodeLocation, OpcodeLocation + 8, Reloc))
  501. {
  502. // Optimize MOVE.x var.L,ofs(reg) into
  503. // MOVE.x var.W(%PC),ofs(reg)
  504. // We cannot handle this above because of the
  505. // offset, which comes after the reloc.
  506. 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)
  507. && (!((Opcode [0] & M68K_MOVE_ABS_OFSREG_INV_0_MASK_0) == M68K_MOVE_ABS_OFSREG_INV_0_0)))
  508. {
  509. OptimizeInfo->OptimizeMovesResult++;
  510. if (OptimizeInfo->NearAssemblyResult >= 0)
  511. OptimizeInfo->NearAssemblyResult += 2;
  512. if (OptimizeInfo->OptimizeMoves)
  513. {
  514. // Turn the opcode into a pc-relative one.
  515. M68K_MAKE_REL_OPCODE_1 (Opcode [1]);
  516. // Move the offset to the correct place.
  517. Opcode [4] = Opcode [6];
  518. Opcode [5] = Opcode [7];
  519. // Change the reloc to 2-byte relative.
  520. Reloc->Relative = TRUE;
  521. Reloc->Size = 2;
  522. Optimized = TRUE;
  523. // Cut or fill the gained space.
  524. M68kCutOrFillRange (Section, OpcodeLocation + 6, OpcodeLocation + 8, OptimizeInfo);
  525. }
  526. }
  527. }
  528. {
  529. OpcodeLocation = RelocLocation - 4;
  530. Opcode = Data + OpcodeLocation;
  531. if (!Optimized && IsBinaryDataRange (Section, OpcodeLocation, OpcodeLocation + 8, Reloc))
  532. {
  533. // Optimize MOVEM.x var.L,regs into
  534. // MOVEM var.W(%PC),regs.
  535. 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))
  536. {
  537. OptimizeInfo->OptimizeMovesResult++;
  538. if (OptimizeInfo->NearAssemblyResult >= 0)
  539. OptimizeInfo->NearAssemblyResult += 2;
  540. if (OptimizeInfo->OptimizeMoves)
  541. {
  542. // Turn the opcode into a pc-relative one.
  543. M68K_MAKE_REL_OPCODE_1 (Opcode [1]);
  544. // Change the reloc to 2-byte relative.
  545. Reloc->Relative = TRUE;
  546. Reloc->Size = 2;
  547. // Cut or fill the gained space.
  548. M68kCutOrFillRange (Section, OpcodeLocation + 6, OpcodeLocation + 8, OptimizeInfo);
  549. }
  550. }
  551. // Optimize BTST #num,var.L into
  552. // BTST #num,var.W(%PC).
  553. else if ((Opcode [0] == M68K_BTST_IMM_ABS_0) && (Opcode [1] == M68K_BTST_IMM_ABS_1) && (Opcode [2] == M68K_BTST_IMM_ABS_2))
  554. {
  555. OptimizeInfo->OptimizeTestsResult++;
  556. if (OptimizeInfo->NearAssemblyResult >= 0)
  557. OptimizeInfo->NearAssemblyResult += 2;
  558. if (OptimizeInfo->OptimizeTests)
  559. {
  560. // Turn the opcode into a pc-relative one.
  561. M68K_MAKE_REL_OPCODE_1 (Opcode [1]);
  562. // Change the reloc to 2-byte relative.
  563. Reloc->Relative = TRUE;
  564. Reloc->Size = 2;
  565. // Cut or fill the gained space.
  566. M68kCutOrFillRange (Section, OpcodeLocation + 6, OpcodeLocation + 8, OptimizeInfo);
  567. }
  568. }
  569. }
  570. }
  571. }
  572. else
  573. {
  574. // The target is not near enough. This means that near
  575. // assembly is not possible.
  576. OptimizeInfo->NearAssemblyResult = -1;
  577. }
  578. }
  579. }
  580. }
  581. }
  582. }
  583. }
  584. // Checks if a specific reloc might be optimizable. This is currently
  585. // limited to 4-bytes absolute relocs because that is the only case
  586. // this is needed for.
  587. BOOLEAN M68kIsRelocOptimizable (const RELOC *Reloc)
  588. {
  589. if (Reloc->Unoptimizable) return FALSE;
  590. if (Reloc->Size == 4 && (!(Reloc->Relative)))
  591. {
  592. const SECTION *Section = Reloc->Parent;
  593. // Most opcodes are two bytes long, and the reloc follows
  594. // immediately.
  595. OFFSET OpcodeLocation = Reloc->Location - 2;
  596. // Safety check before accessing the section data.
  597. if (IsBinaryDataRange (Section, OpcodeLocation, OpcodeLocation + 6, Reloc))
  598. {
  599. const I1 *Opcode = Section->Data + OpcodeLocation;
  600. // *** Branch Optimization ***
  601. if (((Opcode [0] == M68K_JMP_0) && (Opcode [1] == M68K_JMP_1))
  602. || ((Opcode [0] == M68K_JSR_0) && (Opcode [1] == M68K_JSR_1))
  603. // *** Move Optimization ***
  604. || (((Opcode [0] & M68K_LEA_ABS_MASK_0) == M68K_LEA_ABS_0) && ((Opcode [1] & M68K_LEA_ABS_MASK_1) == M68K_LEA_ABS_1))
  605. || ((Opcode [0] == M68K_PEA_ABS_0) && (Opcode [1] == M68K_PEA_ABS_1))
  606. || (((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)
  607. && (!((Opcode [0] & M68K_MOVE_ABS_REG_INV_0_MASK_0) == M68K_MOVE_ABS_REG_INV_0_0))
  608. && (!(((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))))
  609. || (((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)
  610. && (!((Opcode [0] & M68K_MOVE_ABS_PREDEC_INV_0_MASK_0) == M68K_MOVE_ABS_PREDEC_INV_0_0)))
  611. || (((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)
  612. && (!((Opcode [0] & M68K_MOVE_ABS_OFSREG_INV_0_MASK_0) == M68K_MOVE_ABS_OFSREG_INV_0_0)))
  613. || (((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))
  614. // *** Test Optimization ***
  615. || (((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)
  616. && (!((Opcode [1] & M68K_CMP_ABS_REG_INV_0_MASK_1) == M68K_CMP_ABS_REG_INV_0_1)))
  617. || (((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))
  618. || ((Opcode [0] == M68K_BTST_IMM_ABS_0) && (Opcode [1] == M68K_BTST_IMM_ABS_1) && (Opcode [2] == M68K_BTST_IMM_ABS_2))
  619. // *** Calculation Optimization ***
  620. || (((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))
  621. || (((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))
  622. || (((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))
  623. || (((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)))
  624. return TRUE;
  625. }
  626. }
  627. return FALSE;
  628. }
  629. // If the section ends with exactly one NOP instruction, remove the NOP.
  630. void M68kRemoveTrailingNOP (SECTION *Section)
  631. {
  632. I1 *Data = Section->Data;
  633. SIZE Size = Section->Size;
  634. // Validate basic circumstances.
  635. if (Data && (Size >= 4)
  636. // Check for NOP in last instruction.
  637. && (Data [Size - 2] == M68K_NOP_0) && (Data [Size - 1] == M68K_NOP_1) && (IsBinaryDataRange (Section, Size - 2, Size, NULL))
  638. // Check for NOP in previous instruction (which would mean that we
  639. // actually want the NOPs to be there).
  640. && (!((Data [Size - 4] == M68K_NOP_0) && (Data [Size - 3] == M68K_NOP_1)))
  641. // Check if something prevents us from shrinking the section.
  642. && (CanShrinkSection (Section, Size - 2, NULL)))
  643. // Cut the NOP away.
  644. CutSection (Section, Size - 2);
  645. }
  646. // Fix and return the target offset of a reloc.
  647. // In particular, for 1-byte relative relocs, the target offset is
  648. // increased by 1, so the the reloc points to the symbol and not to
  649. // the symbol minus 1.
  650. OFFSET M68kFixTargetOffset (OFFSET Offset, SIZE RelocSize, BOOLEAN RelocRelative)
  651. {
  652. if (RelocRelative && (RelocSize == 1))
  653. return Offset + 1;
  654. else
  655. return Offset;
  656. }
  657. #define SHORT_IMPORTANCE 2048
  658. // Called by M68kGetSectionRelationship; see below.
  659. static COUNT M68kGetRelocImportance (const RELOC *Reloc, OFFSET Offset)
  660. {
  661. SECTION *Section = Reloc->Parent;
  662. if (Reloc->Unoptimizable) return 0;
  663. // Byte offsets are useful only for jumps or branches, so detect
  664. // them.
  665. if (M68K_REL_OK (Offset, 1))
  666. {
  667. OFFSET RelocLocation = Reloc->Location;
  668. OFFSET OpcodeLocation = RelocLocation - 2;
  669. I1 *Opcode = Section->Data + OpcodeLocation;
  670. if (Reloc->Size == 4)
  671. {
  672. // Check whether the reloc belongs to a branch.
  673. BOOLEAN IsJMP = ((Opcode [0] == M68K_JMP_0)
  674. && (Opcode [1] == M68K_JMP_1));
  675. BOOLEAN IsJSR = ((Opcode [0] == M68K_JSR_0)
  676. && (Opcode [1] == M68K_JSR_1));
  677. if ((IsJMP || IsJSR)
  678. && IsBinaryDataRange (Section,
  679. OpcodeLocation,
  680. OpcodeLocation + 6, Reloc))
  681. {
  682. return ((Offset == 4 && IsJMP) ? SHORT_IMPORTANCE * 2
  683. : SHORT_IMPORTANCE);
  684. }
  685. }
  686. else if (Reloc->Size == 2)
  687. {
  688. // Check whether the reloc belongs to a branch.
  689. if ((Opcode [0] & M68K_Bcc_MASK_0) == M68K_Bcc_W_0
  690. && Opcode [1] == M68K_Bcc_W_1
  691. && IsBinaryDataRange (Section,
  692. OpcodeLocation,
  693. OpcodeLocation + 4, Reloc))
  694. {
  695. return (Offset == 2 && !(Opcode [0] == M68K_BSR_W_0
  696. && Opcode [1] == M68K_BSR_W_1)) ? SHORT_IMPORTANCE * 2
  697. : SHORT_IMPORTANCE;
  698. }
  699. }
  700. }
  701. // Everything else is not optimizable.
  702. return 0;
  703. }
  704. // Determines the amount of relationship between the two sections, for the
  705. // situation that Section2 might be put just behind Section1. A reference
  706. // that could potentially be short gets 2048 points; a reference that could
  707. // potentially be removed gets twice as much.
  708. // The effect is that a section with one potentially short reference can
  709. // be at most 2048 bytes long for being inserted immediately by reordering,
  710. // and so on.
  711. COUNT M68kGetSectionRelationship (const SECTION *Section1, const SECTION *Section2)
  712. {
  713. COUNT Result = 0;
  714. const RELOC *Reloc;
  715. for_each (Reloc, Section1->Relocs)
  716. {
  717. const SYMBOL *TargetSymbol = Reloc->Target.Symbol;
  718. if (TargetSymbol && TargetSymbol->Parent == Section2)
  719. Result += M68kGetRelocImportance (Reloc, Section1->Size - Reloc->Location + TargetSymbol->Location + Reloc->Target.Offset + Reloc->FixedOffset);
  720. }
  721. for_each (Reloc, Section2->Relocs)
  722. {
  723. const SYMBOL *TargetSymbol = Reloc->Target.Symbol;
  724. if (TargetSymbol && TargetSymbol->Parent == Section1)
  725. Result += M68kGetRelocImportance (Reloc, TargetSymbol->Location + Reloc->Target.Offset + Reloc->FixedOffset - Section1->Size - Reloc->Location);
  726. }
  727. return Result;
  728. }
  729. // Compute an estimate of how important it is to put the section containing this
  730. // reloc next during local section reordering.
  731. // Here are the estimates used:
  732. // 0-byte branches save 6 bytes and 1 reloc and cannot be deferred -> 512 points
  733. // 2-byte branches save 4 bytes and 1 reloc and can rarely be deferred -> 256
  734. // PC-relative references save 2 bytes and 1 reloc. They can be deferred based
  735. // on how far the accumulated distance is. We compute between 0 and 32 points
  736. // based on the offset, with the formula: (offset^2>>25)+2.
  737. COUNT M68kComputeRelocGoodness(OFFSET Offset, RELOC *Reloc)
  738. {
  739. SECTION *Section = Reloc->Parent;
  740. if (Reloc->Unoptimizable) return 0;
  741. // Byte offsets are useful only for jumps or branches, so detect
  742. // them.
  743. if (M68K_REL_OK (Offset, 1))
  744. {
  745. OFFSET RelocLocation = Reloc->Location;
  746. OFFSET OpcodeLocation = RelocLocation - 2;
  747. I1 *Opcode = Section->Data + OpcodeLocation;
  748. if (Reloc->Size == 4)
  749. {
  750. // Check whether the reloc belongs to a branch.
  751. BOOLEAN IsJMP = ((Opcode [0] == M68K_JMP_0)
  752. && (Opcode [1] == M68K_JMP_1));
  753. BOOLEAN IsJSR = ((Opcode [0] == M68K_JSR_0)
  754. && (Opcode [1] == M68K_JSR_1));
  755. if ((IsJMP || IsJSR)
  756. && IsBinaryDataRange (Section,
  757. OpcodeLocation,
  758. OpcodeLocation + 6, Reloc))
  759. {
  760. return (Offset == 4 && IsJMP) ? 512 : 256;
  761. }
  762. }
  763. else if (Reloc->Size == 2)
  764. {
  765. // Check whether the reloc belongs to a branch.
  766. if ((Opcode [0] & M68K_Bcc_MASK_0) == M68K_Bcc_W_0
  767. && Opcode [1] == M68K_Bcc_W_1
  768. && IsBinaryDataRange (Section,
  769. OpcodeLocation,
  770. OpcodeLocation + 4, Reloc))
  771. {
  772. return (Offset == 2 && !(Opcode [0] == M68K_BSR_W_0
  773. && Opcode [1] == M68K_BSR_W_1)) ? 512
  774. : 256;
  775. }
  776. }
  777. }
  778. // Word offsets are useful everywhere where a PC-relative reference is
  779. // possible. So look for those places.
  780. if (M68K_REL_OK (Offset, 2))
  781. {
  782. OFFSET RelocLocation = Reloc->Location;
  783. OFFSET OpcodeLocation = RelocLocation - 2;
  784. I1 *Opcode = Section->Data + OpcodeLocation;
  785. // Safety check before accessing the section data.
  786. if (Reloc->Size == 4 && IsBinaryDataRange (Section, OpcodeLocation,
  787. OpcodeLocation + 6, Reloc))
  788. {
  789. // Check whether the reloc belongs to a branch.
  790. if (((Opcode [0] == M68K_JMP_0) && (Opcode [1] == M68K_JMP_1))
  791. || ((Opcode [0] == M68K_JSR_0) && (Opcode [1] == M68K_JSR_1))
  792. // Optimize LEA(.L) var.L,reg into
  793. // LEA(.L) var.W(%PC),reg.
  794. || (((Opcode [0] & M68K_LEA_ABS_MASK_0) == M68K_LEA_ABS_0) && ((Opcode [1] & M68K_LEA_ABS_MASK_1) == M68K_LEA_ABS_1))
  795. // Optimize PEA(.L) var.L into PEA(.L) var.W(%PC).
  796. || ((Opcode [0] == M68K_PEA_ABS_0) && (Opcode [1] == M68K_PEA_ABS_1))
  797. // Optimize MOVE.x var.L,reg/(reg)/(reg)+ into
  798. // MOVE.x var.W(%PC),reg/(reg)/(reg)+.
  799. || (((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)
  800. && (!((Opcode [0] & M68K_MOVE_ABS_REG_INV_0_MASK_0) == M68K_MOVE_ABS_REG_INV_0_0))
  801. && (!(((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))))
  802. // Optimize MOVE.x var.L,-(reg) into
  803. // MOVE.x var.W(%PC),-(reg).
  804. || (((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)
  805. && (!((Opcode [0] & M68K_MOVE_ABS_PREDEC_INV_0_MASK_0) == M68K_MOVE_ABS_PREDEC_INV_0_0)))
  806. // Optimize CMP.x var.L,reg into CMP.x var.W(%PC),reg.
  807. || (((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)
  808. && (!((Opcode [1] & M68K_CMP_ABS_REG_INV_0_MASK_1) == M68K_CMP_ABS_REG_INV_0_1)))
  809. // Optimize BTST reg,var.L into BTST reg,var.W(%PC).
  810. || (((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))
  811. // Optimize ADD/SUB.x var.L,reg into
  812. // ADD/SUB.x var.W(%PC),reg.
  813. || (((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))
  814. || (((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))
  815. // Optimize MUL/DIV.x var.L,reg into
  816. // MUL/DIV.x var.W(%PC),reg.
  817. || (((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))
  818. // Optimize AND/OR.x var.L,reg into
  819. // AND/OR.x var.W(%PC),reg.
  820. || (((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)))
  821. {
  822. return ((Offset * Offset) >> 25);
  823. }
  824. }
  825. }
  826. // Everything else is not optimizable, so ignore it for section reordering.
  827. return 0;
  828. }