aopt.pas 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. {
  2. $Id$
  3. Copyright (c) 1999 by Jonas Maebe, member of the Free Pascal
  4. Development Team
  5. This unit contains the interface routines between the code generator
  6. and the optimizer.
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. ****************************************************************************
  19. }
  20. Unit aopt;
  21. Interface
  22. Uses Aasm, cobjects, aoptmsc, aoptda, aoptcs, aoptpeep
  23. {$ifdef i386}
  24. , ao386msc;
  25. {$endif i386}
  26. Type
  27. TAsmOptimizer = Object
  28. { the PAasmOutput list this optimizer instance works on }
  29. AsmL: PAasmOutput;
  30. { The labeltable contains the addresses of the Pai objects that are labels }
  31. LabelInfo: PLabelInfo;
  32. { Start and end of the block that is currently being optimized, initialized }
  33. { by InitDFA }
  34. BlockStart, BlockEnd: Pai;
  35. { How many instructions are between the current instruction and the last one }
  36. { that modified the register }
  37. NrOfInstrSinceLastMod: TInstrSinceLastMod;
  38. { _AsmL is the PAasmOutpout list that has to be optimized }
  39. Constructor Init(_AsmL: PAasmOutput);
  40. { general, processor independent procedures }
  41. { general, processor dependent procedures }
  42. Implementation
  43. Constructor TAsmOptimizer.Init(_AsmL: PAasmOutput);
  44. Begin
  45. AsmL := _AsmL;
  46. End;
  47. Procedure Optimize;
  48. Var HP: Pai;
  49. AsmDFA: TAsmDFA;
  50. Begin
  51. {setup labeltable, always necessary}
  52. BlockStart := Pai(AsmL^.First);
  53. AsmDFA.Init(AsmL, BlockStart, BlockEnd);
  54. {Blockend now either contains an ait_marker with Kind = AsmBlockStart, or nil}
  55. While Assigned(BlockStart) Do
  56. Begin
  57. LabelInfo := AsmDFA.GetLabelInfo;
  58. { peephole optimizations, twice }
  59. PeepHoleOptPass1;
  60. PeepHoleOptPass1;
  61. If (cs_slowoptimize in aktglobalswitches) Then
  62. Begin
  63. { data flow analyzer }
  64. DFAPass2;
  65. { common subexpression elimination }
  66. CSE;
  67. End;
  68. { more peephole optimizations }
  69. PeepHoleOptPass2;
  70. {dispose labeltabel}
  71. AsmDFA.Done;
  72. {continue where we left off, BlockEnd is either the start of an assembler
  73. block or nil}
  74. BlockStart := BlockEnd;
  75. While Assigned(BlockStart) And
  76. (BlockStart^.typ = ait_Marker) And
  77. (Pai_Marker(BlockStart)^.Kind = AsmBlockStart) Do
  78. Begin
  79. {we stopped at an assembler block, so skip it}
  80. While GetNextInstruction(BlockStart, BlockStart) And
  81. ((BlockStart^.Typ <> Ait_Marker) Or
  82. (Pai_Marker(Blockstart)^.Kind <> AsmBlockEnd)) Do;
  83. {blockstart now contains a pai_marker(asmblockend)}
  84. If GetNextInstruction(BlockStart, HP) And
  85. ((HP^.typ <> ait_Marker) Or
  86. (Pai_Marker(HP)^.Kind <> AsmBlockStart)) Then
  87. {there is no assembler block anymore after the current one, so
  88. optimize the next block of "normal" instructions}
  89. AsmDFA.Init(AsmL, BlockStart, BlockEnd);
  90. {otherwise, skip the next assembler block}
  91. Else BlockStart := HP;
  92. End
  93. End;
  94. End;
  95. Destructor TAsmOptimizer.Done;
  96. Begin
  97. End;
  98. {Virtual methods, most have to be overridden by processor dependent methods}
  99. {
  100. $Log$
  101. Revision 1.1 1999-08-08 13:24:50 jonas
  102. + added copyright header/GNU license info
  103. * made the assembler optimizer almost completely OOP
  104. * some code style clean up and extra comments
  105. * moved from the new/aopt to the /new and /new/i386 dirs
  106. }